Object-Oriented Programming (OOP) in python BAKHAT ALI Institute of Geoinformatics and Earth Observation, Pir Mehr Ali Shah Arid Agriculture University Rawalpindi , Punjab, Pakistan bakhtali21uaar@gmail.com
Object-Oriented Programming (OOP) Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" to encapsulate data and functionality, promoting organized code and efficient data reuse.
Key Features of OOP Encapsulation: Protects and bundles data and methods together, ensuring outside interference is limited. Abstraction: Simplifies complex systems by exposing only essential features while hiding unnecessary details. Inheritance: Allows one class to inherit attributes and behaviors from another, enabling code reuse and creating a class hierarchy. Polymorphism: Grants objects the ability to be treated as instances of their parent class, while allowing their specific implementations to define how actions are executed.
Classes and Objects Classes A class is a blueprint to create objects, comprising attributes (data) and methods (functions). Objects An object is an instantiation of a class, representing a specific instance with defined attributes.
Class and Object Syntax class ClassName: # Define a class def __init__(self, param): # Constructor self.attribute = param # Instance variable def method_name(self): # Method definition return self.attribute # Creating an object (instantiation) object_instance = ClassName(value) Example of Class and Object class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year def display_info(self): return f"{self.year} {self.make} {self.model}" # Instantiating the object my_car = Car("Toyota", "Corolla", 2021) print(my_car.display_info()) # Output: 2021 Toyota Corolla
. Inheritance Inheritance allows one class (child class) to inherit the properties and methods of another (parent class).
Types of Inheritance SINGLE INHERITANCE: A CHILD CLASS INHERITS FROM ONE PARENT CLASS. class Animal: def sound(self): return "Some sound" class Dog(Animal): def sound(self): return "Bark" dog = Dog() print(dog.sound()) # Output: Bark
Multilevel Inheritance: A class inherits from a derived class. CLASS PUPPY(DOG): DEF SOUND(SELF): RETURN "YIP" PUPPY = PUPPY() PRINT(PUPPY.SOUND()) # OUTPUT: YIP
Hierarchical Inheritance: Multiple classes inherit from one parent class. class Vehicle: def type(self): return "Vehicle" class Car(Vehicle): def type(self): return "Car" class Bike(Vehicle): def type(self): return "Bike" car = Car() bike = Bike() print(car.type()) # Output: Car print(bike.type()) # Output: Bike
Multiple Inheritance: A class can inherit from multiple parent classes. class A: def method_a(self): return "Method A" class B: def method_b(self): return "Method B" class C(A, B): def method_c(self): return "Method C" obj = C() print(obj.method_a()) # Output: Method A print(obj.method_b()) # Output: Method B
Polymorphism POLYMORPHISM ALLOWS METHODS TO DO DIFFERENT THINGS BASED ON THE OBJECT TYPE OR CLASS INVOKING THEM.
Compile-time Polymorphism (Static Binding): Achieved mainly through method overloading. lass Math: def add(self, a, b): return a + b def add_with_three(self, a, b, c): return a + b + c math = Math() print(math.add(5, 10)) # Output: 15 print(math.add_with_three(5, 10, 15)) # Output: 30
Run-time Polymorphism (Dynamic Binding): Achieved through method overriding. lass Shape: def area(self): pass class Rectangle(Shape): def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * (self.radius**2) shapes = [Rectangle(10, 5), Circle(7)] for shape in shapes: print(shape.area()) # Output: 50, 153.86
Encapsulation ENCAPSULATION COMBINES ATTRIBUTES AND METHODS IN A CLASS AND RESTRICTS ACCESS USING ACCESS MODIFIERS.
class BankAccount: def __init__(self, balance): self.__balance = balance # Private attribute def deposit(self, amount): self.__balance += amount def withdraw(self, amount): if amount <= self.__balance: self.__balance -= amount else: print("Insufficient funds.") def get_balance(self): return self.__balance account = BankAccount(1000) account.deposit(500) print(account.get_balance()) # Output: 1500 account.withdraw(200) print(account.get_balance()) # Output: 1300
Abstraction ABSTRACTION SIMPLIFIES COMPLEX SYSTEMS BY HIDING UNNECESSARY DETAILS AND EXPOSING ONLY THE NECESSARY PARTS USING ABSTRACT CLASSES.
from abc import ABC, abstractmethod class Device(ABC): @abstractmethod def turn_on(self): pass class Television(Device): def turn_on(self): return "TV is now ON." class Radio(Device): def turn_on(self): return "Radio is now ON." tv = Television() radio = Radio() print(tv.turn_on()) # Output: TV is now ON. print(radio.turn_on()) # Output: Radio is now ON.
Comprehensive OOP Features Table with Code Examples Feature Definition Syntax Example Code Example Class A blueprint for objects. class ClassName: class Vehicle: pass Object An instance of a class. object_name = ClassName() my_car = Vehicle() Attributes Data stored in an object. self.attribute_name = value self.make = "Toyota" Methods Functions defined inside a class. def method_name(self): def display_info(self): return self.make Constructor A method called when an object is created. def __init__(self, parameters): def __init__(self, make, model): self.make = make Encapsulation Restricting access to object data. Use of underscores: self.__private_data self.__balance = 1000 Inheritance Deriving new classes from existing ones. class DerivedClass(ParentClass): class Dog(Animal): Polymorphism Methods behaving differently based on the object. Method Overriding class Cat(Animal): def sound(self): return "Meow" Abstraction Hiding complex implementation details. Use of abstract methods from abc import ABC, abstractmethod
OOP Summary: Encapsulation: Protects and bundles data/functionality. Abstraction: Simplifies interfaces. Inheritance: Supports code reuse and hierarchy. Polymorphism: Flexible interfaces for objects.

Object-Oriented Programming (OO) in pythonP) in python.pptx

  • 1.
    Object-Oriented Programming (OOP) in python BAKHATALI Institute of Geoinformatics and Earth Observation, Pir Mehr Ali Shah Arid Agriculture University Rawalpindi , Punjab, Pakistan bakhtali21uaar@gmail.com
  • 2.
    Object-Oriented Programming (OOP) Object-Oriented Programming(OOP) is a programming paradigm that uses "objects" to encapsulate data and functionality, promoting organized code and efficient data reuse.
  • 3.
    Key Features ofOOP Encapsulation: Protects and bundles data and methods together, ensuring outside interference is limited. Abstraction: Simplifies complex systems by exposing only essential features while hiding unnecessary details. Inheritance: Allows one class to inherit attributes and behaviors from another, enabling code reuse and creating a class hierarchy. Polymorphism: Grants objects the ability to be treated as instances of their parent class, while allowing their specific implementations to define how actions are executed.
  • 4.
    Classes and Objects Classes Aclass is a blueprint to create objects, comprising attributes (data) and methods (functions). Objects An object is an instantiation of a class, representing a specific instance with defined attributes.
  • 5.
    Class and ObjectSyntax class ClassName: # Define a class def __init__(self, param): # Constructor self.attribute = param # Instance variable def method_name(self): # Method definition return self.attribute # Creating an object (instantiation) object_instance = ClassName(value) Example of Class and Object class Car: def __init__(self, make, model, year): self.make = make self.model = model self.year = year def display_info(self): return f"{self.year} {self.make} {self.model}" # Instantiating the object my_car = Car("Toyota", "Corolla", 2021) print(my_car.display_info()) # Output: 2021 Toyota Corolla
  • 6.
    . Inheritance Inheritance allowsone class (child class) to inherit the properties and methods of another (parent class).
  • 7.
    Types of Inheritance SINGLEINHERITANCE: A CHILD CLASS INHERITS FROM ONE PARENT CLASS. class Animal: def sound(self): return "Some sound" class Dog(Animal): def sound(self): return "Bark" dog = Dog() print(dog.sound()) # Output: Bark
  • 8.
    Multilevel Inheritance: A classinherits from a derived class. CLASS PUPPY(DOG): DEF SOUND(SELF): RETURN "YIP" PUPPY = PUPPY() PRINT(PUPPY.SOUND()) # OUTPUT: YIP
  • 9.
    Hierarchical Inheritance: Multipleclasses inherit from one parent class. class Vehicle: def type(self): return "Vehicle" class Car(Vehicle): def type(self): return "Car" class Bike(Vehicle): def type(self): return "Bike" car = Car() bike = Bike() print(car.type()) # Output: Car print(bike.type()) # Output: Bike
  • 10.
    Multiple Inheritance: Aclass can inherit from multiple parent classes. class A: def method_a(self): return "Method A" class B: def method_b(self): return "Method B" class C(A, B): def method_c(self): return "Method C" obj = C() print(obj.method_a()) # Output: Method A print(obj.method_b()) # Output: Method B
  • 11.
    Polymorphism POLYMORPHISM ALLOWS METHODSTO DO DIFFERENT THINGS BASED ON THE OBJECT TYPE OR CLASS INVOKING THEM.
  • 12.
    Compile-time Polymorphism (StaticBinding): Achieved mainly through method overloading. lass Math: def add(self, a, b): return a + b def add_with_three(self, a, b, c): return a + b + c math = Math() print(math.add(5, 10)) # Output: 15 print(math.add_with_three(5, 10, 15)) # Output: 30
  • 13.
    Run-time Polymorphism (DynamicBinding): Achieved through method overriding. lass Shape: def area(self): pass class Rectangle(Shape): def __init__(self, length, width): self.length = length self.width = width def area(self): return self.length * self.width class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): return 3.14 * (self.radius**2) shapes = [Rectangle(10, 5), Circle(7)] for shape in shapes: print(shape.area()) # Output: 50, 153.86
  • 14.
    Encapsulation ENCAPSULATION COMBINES ATTRIBUTESAND METHODS IN A CLASS AND RESTRICTS ACCESS USING ACCESS MODIFIERS.
  • 15.
    class BankAccount: def __init__(self,balance): self.__balance = balance # Private attribute def deposit(self, amount): self.__balance += amount def withdraw(self, amount): if amount <= self.__balance: self.__balance -= amount else: print("Insufficient funds.") def get_balance(self): return self.__balance account = BankAccount(1000) account.deposit(500) print(account.get_balance()) # Output: 1500 account.withdraw(200) print(account.get_balance()) # Output: 1300
  • 16.
    Abstraction ABSTRACTION SIMPLIFIES COMPLEXSYSTEMS BY HIDING UNNECESSARY DETAILS AND EXPOSING ONLY THE NECESSARY PARTS USING ABSTRACT CLASSES.
  • 17.
    from abc importABC, abstractmethod class Device(ABC): @abstractmethod def turn_on(self): pass class Television(Device): def turn_on(self): return "TV is now ON." class Radio(Device): def turn_on(self): return "Radio is now ON." tv = Television() radio = Radio() print(tv.turn_on()) # Output: TV is now ON. print(radio.turn_on()) # Output: Radio is now ON.
  • 18.
    Comprehensive OOP FeaturesTable with Code Examples Feature Definition Syntax Example Code Example Class A blueprint for objects. class ClassName: class Vehicle: pass Object An instance of a class. object_name = ClassName() my_car = Vehicle() Attributes Data stored in an object. self.attribute_name = value self.make = "Toyota" Methods Functions defined inside a class. def method_name(self): def display_info(self): return self.make Constructor A method called when an object is created. def __init__(self, parameters): def __init__(self, make, model): self.make = make Encapsulation Restricting access to object data. Use of underscores: self.__private_data self.__balance = 1000 Inheritance Deriving new classes from existing ones. class DerivedClass(ParentClass): class Dog(Animal): Polymorphism Methods behaving differently based on the object. Method Overriding class Cat(Animal): def sound(self): return "Meow" Abstraction Hiding complex implementation details. Use of abstract methods from abc import ABC, abstractmethod
  • 19.
    OOP Summary: Encapsulation: Protects andbundles data/functionality. Abstraction: Simplifies interfaces. Inheritance: Supports code reuse and hierarchy. Polymorphism: Flexible interfaces for objects.