Classes and Objects
Classes and Objects
• definea superclass
class super_class:
# attributes and method definition
inheritance
class sub_class(super_class):
# attributes and method of super_class
# attributes and method of sub_class
• class Animal:
# create an object of the subclass
• # a ribute and method of the labrador = Dog()
parent class
• name = "" # access superclass a ribute and method
• labrador.name = "Rohu"
• def eat(self): labrador.eat()
• print("I can eat")
# call subclass method
• # inherit from Animal labrador.display()
• class Dog(Animal):
• # new method in subclass
• def display(self):
• # access name a ribute of
superclass using self
• print("My name is ",
self.name)
• class Animal: #Base class
• def speak(self):
• print("Animal Speaking")
• #The child class Dog inherits the base class Animal
• class Dog(Animal):
• def bark(self):
• print("dog barking")
• #The child class Dogchild inherits another child class Dog
• class DogChild(Dog):
• def eat(self):
• print("Eating bread...")
• d = DogChild()
• d.bark()
Inheritance Types
• There are 5 di erent types of inheritance in Python. They are:
• Single Inheritance: a child class inherits from only one parent
class.
• Multiple Inheritance: a child class inherits from multiple parent
classes.
• Multilevel Inheritance: a child class inherits from its parent
class, which is inheriting from its parent class.
• Hierarchical Inheritance: more than one child class are created
from a single parent class.
• Hybrid Inheritance: combines more than one form of
inheritance.
Advantages of Inheritance
• Code Reusability: Since a child class can inherit all the
functionalities of the parent's class, this allows code reusability.
• E cient Development: Once a functionality is developed, we
can simply inherit it which allows for cleaner code and easy
maintenance.
• Customization: Since we can also add our own functionalities
in the child class, we can inherit only the useful functionalities
and de ne other required features.
Method Overriding
• We can provide some speci c
class Calculation1:
def Summation(self,a,b):
implementation
class method in of
ourthe parent
child class. return a+b;
When
de nedthe
in parent
the class
child method
class with is class Calculation2:
some speci c implementation, def Multiplication(self,a,b):
then the concept is called return a*b;
method overriding. class Derived(Calculation1,Calculation2):
class Animal: def Divide(self,a,b):
def speak(self): return a/b;
print("speaking")
d = Derived()
class Dog(Animal):
def speak(self): print(d.Summation(10,20))
print("Barking") print(d.Multiplication(10,20))
print(d.Divide(10,20))
d = Dog() print(isinstance(d,Derived))
Data abstraction in python
class Employee:
• Abstraction is an important __count = 0;
aspect of object-oriented
programming. def __init__(self):
• In python, we can also Employee.__count = Employee.__count+1
perform data hiding by adding def display(self):
the double underscore (___) as print("The number of employees",Employee.__count
a pre x to the a ribute which
is to be hidden. emp = Employee()
• After this, the a ribute will not try:
be visible outside of the class print(emp.__count)
through the object. nally:
emp.display()
Abstraction classes in Python # Python program to de ne
# abstract class
from abc import ABC
• Abstraction is used to hide the internal functionality of class Polygon(ABC):
the function from the users. User may need to kno # abstract method
w "what function does" but they don't know "how it def sides(self):
does." pass
• A class that consists of one or more abstract method
is called the abstract class. Abstract methods do not class Triangle(Polygon):
contain their implementation. Abstract class can be def sides(self):
inherited by the subclass and abstract method gets its print("Triangle has 3 sides")
de nition in the subclass.
• Python provides the abc (Abstract Base classes) class square(Polygon):
module to use the abstraction in the Python program. def sides(self):
• We use the @abstractmethod decorator to de ne an print("I have 4 sides")
abstract method or if we don't provide the de nition to # Driver code
the method, it automatically becomes the abstract t = Triangle()
method. t.sides()