1.4.Classes and Objects
1.4.Classes and Objects
• An object is called an
instance of a class.
• Objectname=
• The object is an entity that
has state and behavior. ClassName()
• It may be any real-world
object like the mouse,
keyboard, chair, table, pen,
etc.
Python Class and Objects
• # define a class
• class Bike:
• name = ""
• gear = 0
s = Student("John",101,22)
print(s.__doc__)
print(s.__dict__)
print(s.__module__)
Python Inheritance
• define a 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
labrador = Dog()
• # attribute and method of the
parent class # access superclass attribute and method
• name = "" labrador.name = "Rohu"
• labrador.eat()
• def eat(self): # call subclass method
• print("I can eat") labrador.display()
• d = DogChild()
• d.bark()
• d.speak()
Inheritance Types
• There are 5 different 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
Advantages of Inheritance
• Code Reusability: Since a child class can inherit all the
functionalities of the parent's class, this allows code
reusability.
• Efficient 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 define other required features.
Method Overriding
class Calculation1:
• We can provide some specific def Summation(self,a,b):
return a+b;
implementation of the parent class
method in our child class. When the class Calculation2:
parent class method is defined in the def Multiplication(self,a,b):
child class with some specific return a*b;
implementation, then the concept is
called method overriding. class Derived(Calculation1,Calculation2):
def Divide(self,a,b):
class Animal:
return a/b;
def speak(self):
print("speaking")
d = Derived()
class Dog(Animal):
print(d.Summation(10,20))
def speak(self):
print(d.Multiplication(10,20))
print("Barking")
print(d.Divide(10,20))
print(isinstance(d,Derived))
d = Dog()
d.speak()
Data abstraction in python
class Employee:
• Abstraction is an important __count = 0;
aspect of object-oriented
programming. def __init__(self):
Employee.__count = Employee.__count+1
• In python, we can also perform
data hiding by adding the double def display(self):
underscore (___) as a prefix to print("The number of employees",Employee.__count)
the attribute which is to be
emp = Employee()
hidden. try:
• After this, the attribute will not print(emp.__count)
finally:
be visible outside of the class emp.display()
through the object.
# Python program to define
Abstraction classes in Python # abstract class
from abc import ABC
s = square()
s.sides()