Inheritance
1
Inheritance allows to define a subclass that inherits all the methods and
properties from parent class.
The benefits of inheritance are:
1. It represents real-world relationships well.
2. It provides reusability of a code. Don’t have to write the same code
again and again. Can add more features to a class without modifying
it.
3. It is transitive in nature, which means that if class B inherits from
another class A, then all the subclasses of B would automatically
inherit from class A.
Inheritance
2
Different forms of Inheritance:
1. Single inheritance: When a child class inherits from only one parent
class, it is called as single inheritance.
2. Multiple inheritance: When a child class inherits from multiple parent
classes, it is called as multiple inheritance.
3. Multilevel inheritance: When we have child and grand child
relationship.
4. Hierarchical inheritance: More than one derived classes are created
from a single base.
5. Hybrid inheritance: This form combines more than one form of
inheritance. Basically, it is a blend of more than one type of
inheritance.
Single Inheritance
3
class Nature:
def rainbow(self):
print("rainbow have 7 colors")
class List(Nature):
def color(self):
print("green is one of the rainbow colors")
d = List()
d.rainbow()
d.color()
Output
rainbow have 7 colors
green is one of the rainbow colors
Multiple Inheritance
4
class Add:
def Summation(self,a,b):
return a+b;
class Mul:
def Multiplication(self,a,b):
return a*b;
class Calci(Add,Mul):
def Divide(self,a,b):
return a/b;
d = Calci()
print(d.Summation(10,20))
print(d.Multiplication(10,20))
print(d.Divide(10,20))
Multilevel Inheritance
5
class Oldmobile:
def m1(self):
print("normal phone")
class Midmobile(Oldmobile):
def m2(self):
print("better phone")
class Smartmobile(Midmobile):
def m3(self):
print("smartmobile")
d = Smartmobile()
d.m1()
d.m2()
d.m3()
Output
normal phone
better phone
smartmobile
Hierarchical Inheritance
6
class College:
def setData(self,id,name):
self.id=id
self.name=name
def showData(self):
print("Id: ",self.id)
print("Name: ", self.name)
class Cse(College):
def setcsedata(self,id,name,dept):
self.setData(id,name)
self.dept=dept
def showcsedata(self):
self.showData()
print("Department: ", self.dept)
Hierarchical Inheritance
7
class Ece(College):
def setecedata(self,id,name,dept):
self.setData(id,name)
self.dept=dept
def showecedata(self):
self.showData()
print("Department: ", self.dept)
print("CSE DEPT")
c=Cse()
c.setcsedata(5,"iare","computers")
c.showcsedata()
print("nECE DEPT")
e = Ece()
e.setecedata(4, "iare", "electronics")
e.showecedata()
Inheritance
8
class Base(object):
def __init__(self, x):
self.x = x
class Derived(Base):
def __init__(self, x, y):
Base.x = x
self.y = y
def printXY(self):
print(Base.x, self.y)
d = Derived(10, 20)
d.printXY()
Inheritance and polymorphism
9
class India:
def intro(self):
print("There are many states in India.")
def capital(self):
print("each state have a capital")
class Ap(India):
def capital(self):
print("Capital of AP is Amaravati")
class Te(India):
def capital(self):
print("Capital of Te is Hyderabad ")
Inheritance and polymorphism
10
i = India()
a = Ap()
t = Te()
i.intro()
i.capital()
a.intro()
a.capital()
t.intro()
t.capital()
Inheritance and polymorphism
11
There are many states in India.
each state have a capital
There are many states in India.
Capital of AP is Amaravati
There are many states in India.
Capital of Te is Hyderabad
Method overriding
12
Method overriding is a concept of object oriented programming that
allows us to change the implementation of a function in the child
class that is defined in the parent class.
Following conditions must be met for overriding a function:
1. Inheritance should be there.
2. The function that is redefined in the child class should have the
same signature as in the parent class i.e. same number of
parameters.
13
class Parent(object):
def __init__(self):
self.value = 5
def get_value(self):
print(self.value)
class Child(Parent):
pass
c=Child()
c.get_value()
Output
5
Method overriding
Method overriding
14
class Parent(object):
def __init__(self):
self.value = 5
def get_value(self):
print(self.value)
class Child(Parent):
def get_value(self):
print(self.value + 1)
c=Child()
c.get_value()
Output
6
Method overriding
15
class Robot:
def action(self):
print('Robot action')
class HelloRobot(Robot):
def action(self):
print('Hello world')
r = HelloRobot()
r.action()
Output
Hello world
Method overriding
16
class Square:
def __init__(self, side):
self.side = side
def area(self):
a=self.side * self.side
print(a)
class Cube(Square):
def area(self):
face_area = self.side * self.side
b=face_area * 6
print(b)
c=Cube(2)
c.area()
Output
24
Super ()
17
super() built-in has two major use cases:
•Allows us to avoid using base class explicitly
•Working with Multiple Inheritance
Syntax
super().methodName(args)
OR
super(subClass, instance).method(args)
Super()
18
class Square:
def __init__(self, side):
self.side = side
def area(self):
a=self.side * self.side
print(a)
class Cube(Square):
def area(self):
super().area()
face_area = self.side * self.side
b=face_area * 6
print(b)
Super()
19
c=Cube(2)
c.area()
Output
4
24
Abstract Classes
20
• An abstract method is a method that has declaration but not has any
implementation.
• A class which contains one or more abstract methods is called an
abstract class.
• Abstract classes are not able to instantiated and it needs subclasses to
provide implementations for those abstract methods which are defined in
abstract classes.
•By defining an abstract base class, common Application Program
Interface(API) can be defined for a set of subclasses.
•A method becomes an abstract by decorated it with a
keyword @abstractmethod.
Abstract Classes
21
• An abstract method is a method that has declaration but
not has any implementation.
from abc import ABC
class MyABC(ABC):
pass
OR
from abc import ABCMeta
class MyABC(metaclass=ABCMeta):
pass
Abstract Classes
22
from abc import ABC, abstractmethod
class Polygon(ABC):
@abstractmethod
def noofsides(self):
pass
class Triangle(Polygon):
def noofsides(self):
print("I have 3 sides")
class Pentagon(Polygon):
def noofsides(self):
print("I have 5 sides")
Abstract Classes
23
class Hexagon(Polygon):
def noofsides(self):
print("I have 6 sides")
class Quadrilateral(Polygon):
def noofsides(self):
print("I have 4 sides")
Abstract Classes
24
R = Triangle()
R.noofsides()
K = Quadrilateral()
K.noofsides()
R = Pentagon()
R.noofsides()
K = Hexagon()
K.noofsides()
Abstract Classes
25
Output:
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
Abstract Classes
26
import abc
class AbstractAnimal(object):
__metaclass__ = abc.ABCMeta
@abc.abstractmethod
def walk(self):
''' data '''
@abc.abstractmethod
def talk(self):
''' data '''
Abstract Classes
27
class Duck(AbstractAnimal):
name = ‘ ‘
def __init__(self, name):
print('duck created.')
self.name = name
def walk(self):
print('walks')
def talk(self):
print('quack')
Abstract Classes
28
obj = Duck('duck1')
obj.talk()
obj.walk()
Output:
duck created
walks
quack
Interfaces
29
•In object oriented programming, an interface is a
set of publicly accessible methods on an object
which can be used by other parts of the program
to interact with that object.
•There’s no keyword in Python.
Interfaces
30
import abc
class Sampleinterface(abc.ABC):
@abc.abstractmethod
def fun(self):
pass
class Example(Sampleinterface):
def fun(self):
print("interface example")
c=Example()
c.fun()
Access specifiers
31
In most of the object-oriented languages access
modifiers are used to limit the access to the
variables and functions of a class.
Most of the languages use three types of access
modifiers, they are -
• Private
• Public
• Protected.
Access specifiers
32
Python makes the use of underscores to specify the
access modifier for a specific data member and
member function in a class.
Access Modifier: Public
The members declared as Public are accessible
from outside the Class through an object of the
class.
Access specifiers
33
Access Modifier: Protected
The members declared as Protected are accessible
from outside the class but only in a class derived
from it that is in the child or subclass.
Access Modifier: Private
These members are only accessible from within the
class. No outside Access is allowed.
Access specifiers
34
Access Modifier: Public
No under score - self.name = name
Access Modifier: Protected
Single under score - self._name = name
Access Modifier: Private
Double under score - self.__name = name
Access specifiers
35
class Company:
def __init__(self, name, proj):
self.name = name
self._proj = proj
def show(self):
print(“IT COMPANY”)
Access specifiers
36
class Emp(Company):
def __init__(self, eName, sal, cName, proj):
Company.__init__(self, cName, proj)
self.name = eName
self.__sal = sal
def show_sal(self):
print("The salary of ",self.name," is ",self.__sal,)
Access specifiers
37
c = Company("TCS", "college")
e = Emp("pandu", 9999666, c.name, c._proj)
print("Welcome to ", c.name)
print("Here",e.name,"is working on",e._proj)
e.show_sal()

Inheritance

  • 1.
    Inheritance 1 Inheritance allows todefine a subclass that inherits all the methods and properties from parent class. The benefits of inheritance are: 1. It represents real-world relationships well. 2. It provides reusability of a code. Don’t have to write the same code again and again. Can add more features to a class without modifying it. 3. It is transitive in nature, which means that if class B inherits from another class A, then all the subclasses of B would automatically inherit from class A.
  • 2.
    Inheritance 2 Different forms ofInheritance: 1. Single inheritance: When a child class inherits from only one parent class, it is called as single inheritance. 2. Multiple inheritance: When a child class inherits from multiple parent classes, it is called as multiple inheritance. 3. Multilevel inheritance: When we have child and grand child relationship. 4. Hierarchical inheritance: More than one derived classes are created from a single base. 5. Hybrid inheritance: This form combines more than one form of inheritance. Basically, it is a blend of more than one type of inheritance.
  • 3.
    Single Inheritance 3 class Nature: defrainbow(self): print("rainbow have 7 colors") class List(Nature): def color(self): print("green is one of the rainbow colors") d = List() d.rainbow() d.color() Output rainbow have 7 colors green is one of the rainbow colors
  • 4.
    Multiple Inheritance 4 class Add: defSummation(self,a,b): return a+b; class Mul: def Multiplication(self,a,b): return a*b; class Calci(Add,Mul): def Divide(self,a,b): return a/b; d = Calci() print(d.Summation(10,20)) print(d.Multiplication(10,20)) print(d.Divide(10,20))
  • 5.
    Multilevel Inheritance 5 class Oldmobile: defm1(self): print("normal phone") class Midmobile(Oldmobile): def m2(self): print("better phone") class Smartmobile(Midmobile): def m3(self): print("smartmobile") d = Smartmobile() d.m1() d.m2() d.m3() Output normal phone better phone smartmobile
  • 6.
    Hierarchical Inheritance 6 class College: defsetData(self,id,name): self.id=id self.name=name def showData(self): print("Id: ",self.id) print("Name: ", self.name) class Cse(College): def setcsedata(self,id,name,dept): self.setData(id,name) self.dept=dept def showcsedata(self): self.showData() print("Department: ", self.dept)
  • 7.
    Hierarchical Inheritance 7 class Ece(College): defsetecedata(self,id,name,dept): self.setData(id,name) self.dept=dept def showecedata(self): self.showData() print("Department: ", self.dept) print("CSE DEPT") c=Cse() c.setcsedata(5,"iare","computers") c.showcsedata() print("nECE DEPT") e = Ece() e.setecedata(4, "iare", "electronics") e.showecedata()
  • 8.
    Inheritance 8 class Base(object): def __init__(self,x): self.x = x class Derived(Base): def __init__(self, x, y): Base.x = x self.y = y def printXY(self): print(Base.x, self.y) d = Derived(10, 20) d.printXY()
  • 9.
    Inheritance and polymorphism 9 classIndia: def intro(self): print("There are many states in India.") def capital(self): print("each state have a capital") class Ap(India): def capital(self): print("Capital of AP is Amaravati") class Te(India): def capital(self): print("Capital of Te is Hyderabad ")
  • 10.
    Inheritance and polymorphism 10 i= India() a = Ap() t = Te() i.intro() i.capital() a.intro() a.capital() t.intro() t.capital()
  • 11.
    Inheritance and polymorphism 11 Thereare many states in India. each state have a capital There are many states in India. Capital of AP is Amaravati There are many states in India. Capital of Te is Hyderabad
  • 12.
    Method overriding 12 Method overridingis a concept of object oriented programming that allows us to change the implementation of a function in the child class that is defined in the parent class. Following conditions must be met for overriding a function: 1. Inheritance should be there. 2. The function that is redefined in the child class should have the same signature as in the parent class i.e. same number of parameters.
  • 13.
    13 class Parent(object): def __init__(self): self.value= 5 def get_value(self): print(self.value) class Child(Parent): pass c=Child() c.get_value() Output 5 Method overriding
  • 14.
    Method overriding 14 class Parent(object): def__init__(self): self.value = 5 def get_value(self): print(self.value) class Child(Parent): def get_value(self): print(self.value + 1) c=Child() c.get_value() Output 6
  • 15.
    Method overriding 15 class Robot: defaction(self): print('Robot action') class HelloRobot(Robot): def action(self): print('Hello world') r = HelloRobot() r.action() Output Hello world
  • 16.
    Method overriding 16 class Square: def__init__(self, side): self.side = side def area(self): a=self.side * self.side print(a) class Cube(Square): def area(self): face_area = self.side * self.side b=face_area * 6 print(b) c=Cube(2) c.area() Output 24
  • 17.
    Super () 17 super() built-inhas two major use cases: •Allows us to avoid using base class explicitly •Working with Multiple Inheritance Syntax super().methodName(args) OR super(subClass, instance).method(args)
  • 18.
    Super() 18 class Square: def __init__(self,side): self.side = side def area(self): a=self.side * self.side print(a) class Cube(Square): def area(self): super().area() face_area = self.side * self.side b=face_area * 6 print(b)
  • 19.
  • 20.
    Abstract Classes 20 • Anabstract method is a method that has declaration but not has any implementation. • A class which contains one or more abstract methods is called an abstract class. • Abstract classes are not able to instantiated and it needs subclasses to provide implementations for those abstract methods which are defined in abstract classes. •By defining an abstract base class, common Application Program Interface(API) can be defined for a set of subclasses. •A method becomes an abstract by decorated it with a keyword @abstractmethod.
  • 21.
    Abstract Classes 21 • Anabstract method is a method that has declaration but not has any implementation. from abc import ABC class MyABC(ABC): pass OR from abc import ABCMeta class MyABC(metaclass=ABCMeta): pass
  • 22.
    Abstract Classes 22 from abcimport ABC, abstractmethod class Polygon(ABC): @abstractmethod def noofsides(self): pass class Triangle(Polygon): def noofsides(self): print("I have 3 sides") class Pentagon(Polygon): def noofsides(self): print("I have 5 sides")
  • 23.
    Abstract Classes 23 class Hexagon(Polygon): defnoofsides(self): print("I have 6 sides") class Quadrilateral(Polygon): def noofsides(self): print("I have 4 sides")
  • 24.
    Abstract Classes 24 R =Triangle() R.noofsides() K = Quadrilateral() K.noofsides() R = Pentagon() R.noofsides() K = Hexagon() K.noofsides()
  • 25.
    Abstract Classes 25 Output: I have3 sides I have 4 sides I have 5 sides I have 6 sides
  • 26.
    Abstract Classes 26 import abc classAbstractAnimal(object): __metaclass__ = abc.ABCMeta @abc.abstractmethod def walk(self): ''' data ''' @abc.abstractmethod def talk(self): ''' data '''
  • 27.
    Abstract Classes 27 class Duck(AbstractAnimal): name= ‘ ‘ def __init__(self, name): print('duck created.') self.name = name def walk(self): print('walks') def talk(self): print('quack')
  • 28.
    Abstract Classes 28 obj =Duck('duck1') obj.talk() obj.walk() Output: duck created walks quack
  • 29.
    Interfaces 29 •In object orientedprogramming, an interface is a set of publicly accessible methods on an object which can be used by other parts of the program to interact with that object. •There’s no keyword in Python.
  • 30.
    Interfaces 30 import abc class Sampleinterface(abc.ABC): @abc.abstractmethod deffun(self): pass class Example(Sampleinterface): def fun(self): print("interface example") c=Example() c.fun()
  • 31.
    Access specifiers 31 In mostof the object-oriented languages access modifiers are used to limit the access to the variables and functions of a class. Most of the languages use three types of access modifiers, they are - • Private • Public • Protected.
  • 32.
    Access specifiers 32 Python makesthe use of underscores to specify the access modifier for a specific data member and member function in a class. Access Modifier: Public The members declared as Public are accessible from outside the Class through an object of the class.
  • 33.
    Access specifiers 33 Access Modifier:Protected The members declared as Protected are accessible from outside the class but only in a class derived from it that is in the child or subclass. Access Modifier: Private These members are only accessible from within the class. No outside Access is allowed.
  • 34.
    Access specifiers 34 Access Modifier:Public No under score - self.name = name Access Modifier: Protected Single under score - self._name = name Access Modifier: Private Double under score - self.__name = name
  • 35.
    Access specifiers 35 class Company: def__init__(self, name, proj): self.name = name self._proj = proj def show(self): print(“IT COMPANY”)
  • 36.
    Access specifiers 36 class Emp(Company): def__init__(self, eName, sal, cName, proj): Company.__init__(self, cName, proj) self.name = eName self.__sal = sal def show_sal(self): print("The salary of ",self.name," is ",self.__sal,)
  • 37.
    Access specifiers 37 c =Company("TCS", "college") e = Emp("pandu", 9999666, c.name, c._proj) print("Welcome to ", c.name) print("Here",e.name,"is working on",e._proj) e.show_sal()