SlideShare a Scribd company logo
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()

More Related Content

PPTX
Module 11 : Inheritance
PPTX
inheritance in C++
PDF
OOP Inheritance
PPT
Inheritance
PDF
Inheritance
PPTX
Inheritance in c++
PPTX
EASY TO LEARN INHERITANCE IN C++
Module 11 : Inheritance
inheritance in C++
OOP Inheritance
Inheritance
Inheritance
Inheritance in c++
EASY TO LEARN INHERITANCE IN C++

What's hot (20)

PPT
inhertance c++
PPTX
Inheritance
PPT
Inheritance
PPTX
C# Inheritance
PPTX
Single inheritance
PPTX
Inheritance in c++theory
PPT
Inheritance OOP Concept in C++.
PPTX
Inheritance in c++ part1
PPTX
Inheritance in OOPS
PPT
Inheritance, Object Oriented Programming
PPT
Inheritance in C++
PPS
Inheritance
PPTX
Inheritance In C++ (Object Oriented Programming)
PPTX
inheritance c++
PPTX
Friend functions
PPTX
Inheritance in oops
PPTX
Inheritance in c++
PPTX
Advance OOP concepts in Python
PPTX
00ps inheritace using c++
PPT
inheritance
inhertance c++
Inheritance
Inheritance
C# Inheritance
Single inheritance
Inheritance in c++theory
Inheritance OOP Concept in C++.
Inheritance in c++ part1
Inheritance in OOPS
Inheritance, Object Oriented Programming
Inheritance in C++
Inheritance
Inheritance In C++ (Object Oriented Programming)
inheritance c++
Friend functions
Inheritance in oops
Inheritance in c++
Advance OOP concepts in Python
00ps inheritace using c++
inheritance
Ad

Similar to Inheritance (20)

PPTX
Python programming computer science and engineering
PDF
Unit 3-Classes ,Objects and Inheritance.pdf
PPTX
PPTX
arthimetic operator,classes,objects,instant
PPTX
Class and Objects in python programming.pptx
PPTX
OOPS 46 slide Python concepts .pptx
PDF
Object oriented Programning Lanuagues in text format.
PDF
All about python Inheritance.python codingdf
PPTX
Python advance
PPTX
Problem solving with python programming OOP's Concept
PDF
Python programming : Inheritance and polymorphism
PPTX
9-_Object_Oriented_Programming_Using_Python 1.pptx
PPTX
object oriented programming(PYTHON)
PPTX
Object Oriented Programming.pptx
PDF
Object Oriented programming Using Python.pdf
PDF
9-_Object_Oriented_Programming_Using_Python.pdf
PDF
Object-Oriented Programming System presentation
PPTX
Inheritance_in_OOP_using Python Programming
PPTX
OOP Concepts Python with code refrences.pptx
Python programming computer science and engineering
Unit 3-Classes ,Objects and Inheritance.pdf
arthimetic operator,classes,objects,instant
Class and Objects in python programming.pptx
OOPS 46 slide Python concepts .pptx
Object oriented Programning Lanuagues in text format.
All about python Inheritance.python codingdf
Python advance
Problem solving with python programming OOP's Concept
Python programming : Inheritance and polymorphism
9-_Object_Oriented_Programming_Using_Python 1.pptx
object oriented programming(PYTHON)
Object Oriented Programming.pptx
Object Oriented programming Using Python.pdf
9-_Object_Oriented_Programming_Using_Python.pdf
Object-Oriented Programming System presentation
Inheritance_in_OOP_using Python Programming
OOP Concepts Python with code refrences.pptx
Ad

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Structure & Organelles in detailed.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
RMMM.pdf make it easy to upload and study
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Basic Mud Logging Guide for educational purpose
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
master seminar digital applications in india
PDF
Pre independence Education in Inndia.pdf
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
01-Introduction-to-Information-Management.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Supply Chain Operations Speaking Notes -ICLT Program
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
RMMM.pdf make it easy to upload and study
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Microbial disease of the cardiovascular and lymphatic systems
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
O7-L3 Supply Chain Operations - ICLT Program
Basic Mud Logging Guide for educational purpose
Module 4: Burden of Disease Tutorial Slides S2 2025
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
master seminar digital applications in india
Pre independence Education in Inndia.pdf
PPH.pptx obstetrics and gynecology in nursing
01-Introduction-to-Information-Management.pdf

Inheritance

  • 1. 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.
  • 2. 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.
  • 3. 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
  • 4. 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))
  • 5. 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
  • 6. 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)
  • 7. 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()
  • 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 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 ")
  • 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 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
  • 12. 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. 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: def action(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-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)
  • 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)
  • 20. 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.
  • 21. 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
  • 22. 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")
  • 23. 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")
  • 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 have 3 sides I have 4 sides I have 5 sides I have 6 sides
  • 26. Abstract Classes 26 import abc class AbstractAnimal(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 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.
  • 30. 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()
  • 31. 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.
  • 32. 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.
  • 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()