07 Introduction to Object-Oriented Programming (OOP).pptx
Document Details
Uploaded by PlentifulMonkey
Universidad Autónoma de Nuevo León
Tags
Full Transcript
INTRODUCTION TO OBJECT- ORIENTED PROGRAMMING (OOP) INTRODUCTION TO OOP Procedure-Oriented Object-Oriented Programming (POP) Programming (OOP) Consists of a list of Breaks programming task instructions orga...
INTRODUCTION TO OBJECT- ORIENTED PROGRAMMING (OOP) INTRODUCTION TO OOP Procedure-Oriented Object-Oriented Programming (POP) Programming (OOP) Consists of a list of Breaks programming task instructions organized into into objects functions Objects combine data Divides program into (attributes) and variables, data structures, behaviors/functions and routines (methods) Good for simple and small Better suited for large programs programs Steeper learning curve but extremely powerful Clear Modular Structure Enhances code reusability Simple Solution to Complex Problems Provides a straightforward approach BENEFITS Abstract Data Types OF OOP Models real-world scenarios effectively Hides Implementation Details Leaves a clearly defined interface Combines Data and Operations Integrates data with corresponding operations CLASSES AND OBJECTS Class as a Blueprint Defines a logical grouping of data and functions Models real-world entities Example of People Class Contains data such as name and age CLASS AND Includes behavior functions to print ages and genders Object as an Instance OBJECT Instance of the class with actual values Example: Person named 'Iron man' with age 35 Class as a Template Defines needed information Independence of Objects EXAMPLE: PEOPLE CLASS Class Class named 'People' with attributes Definitio 'name' and 'age' n Method 'greet' to print a greeting message Object Creating 'person1' with name 'Iron Instantia Man' and age 35 tion Creating 'person2' with name 'Batman' and age 33 Method Calling 'greet' method on 'person1' Invocatio and 'person2' n Printing 'name' and 'age' of 'person1' and 'person2' CLASS Class Blueprint for logical grouping of data and functions Defined as a block of code starting with the class statement Syntax: class ClassName(superclass): Object Instance of the defined class with actual values EXAMPLE: STUDENT CLASS Class Definition Class named Student Attributes: sid, name, gender, type Initialization Method init method initializes attributes sid, name, gender passed in type has a fixed value 'learning' Accessing Attributes Attributes accessed with self.attribute Example: self.name in say_name method Method Definition say_name method prints student name CLASS VS INSTANCE Instanc Belong to a specific instance ATTRIBUTES e Attribut Accessed using self.attribute within the es class Class Shared with all instances created from Attribut the class es Defined using class_name.attribute Exampl Class attribute n to record number of e: objects created Student Method num_instances to print the Class number of instances Instanc sid, name, and gender belong to specific e objects Attribut student1.name is es in Exampl e ADVANCED OOP CONCEPTS INHERITANCE, Inheritance ENCAPSULATION, Makes OOP code more AND modular POLYMORPHISM Facilitates code reuse Builds relationships between classes Encapsulation Hides private details of a class Protects data from other objects Polymorphism Allows common operations in different ways Enhances flexibility in code INHERITANCE Relationship Definition of Class Structure for between Child Example: Sensor Inheritance Inheritance and Parent Class Classes Allows a class to Basic structure: Parent class is Attributes: inherit methods class usually a general name, location, and attributes ClassName(supe type record_date, from another rclass) Child class is a data class Child class can specific type Methods: New class is access all add_data, called child attributes and clear_data class, and the methods from inherited class is the superclass called parent class or superclass INHERITING AND METHOD OVERRIDING Inheriting Method from a parent Benefits Overriding class Allows the Changing Allows child class the modification to use implementat and methods ion of a improvemen and method t of attributes of from the inherited the parent parent class methods class in the child ENCAPSULATION Definition of Encapsulation in Single and Double Encapsulation Python Underscores Restricts access Achieved using Single to methods and private methods underscore for attributes in a or attributes convention class Uses single or Hides complex double details from underscores as users prefix Prevents accidental data modification POLYMORPHISM Definition of Example of Benefits of Polymorphism Polymorphism Polymorphism Means multiple Commonly Reduces forms named methods complexities Allows single across classes Acts differently interface with or child classes in different different Example: situations underlying Overriding forms show_type method in UCBAcc PROBLEMS Classes and Objects Differences between classes and objects Using 'self' in Methods Constructors Class vs Instance Attributes Point Class Distance Calculation Inheritance Inheriting from Superclass Replacing Methods Super Method Modeling Real World Objects Car Class EXAMPLE: CAR Initializes with brand and AND TRUCK CLASSES color Method to start the car Truck Class Inherits from Car class Initializes with brand, color, and size Method to start the truck with key validation Method to stop the truck with brake validation Truck Object Instance of Truck class Starts the truck with the correct key Attempts to stop the truck without brake CONCLUSION Modeling Real Inheritance in Example of a World Objects Python Car Class with Classes Create a class Create a new Define a Car to represent a class that class with real world inherits from attributes and object the existing methods Use attributes class Create a new and methods Incorporate class that to define the concepts inherits from class learned so far Car