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

What's hot (20)

PPT
inhertance c++
abhilashagupta
 
PPTX
Inheritance
Tech_MX
 
PPT
Inheritance
Mustafa Khan
 
PPTX
C# Inheritance
Prem Kumar Badri
 
PPTX
Single inheritance
Äkshäý M S
 
PPTX
Inheritance in c++theory
ProfSonaliGholveDoif
 
PPT
Inheritance OOP Concept in C++.
MASQ Technologies
 
PPTX
Inheritance in c++ part1
Mirza Hussain
 
PPTX
Inheritance in OOPS
Ronak Chhajed
 
PPT
Inheritance, Object Oriented Programming
Arslan Waseem
 
PPT
Inheritance in C++
RAJ KUMAR
 
PPS
Inheritance
Ashish Awasthi
 
PPTX
Inheritance In C++ (Object Oriented Programming)
Gajendra Singh Thakur
 
PPTX
inheritance c++
Muraleedhar Sundararajan
 
PPTX
Friend functions
Megha Singh
 
PPTX
Inheritance in oops
Hirra Sultan
 
PPTX
Inheritance in c++
Paumil Patel
 
PPTX
Advance OOP concepts in Python
Sujith Kumar
 
PPTX
00ps inheritace using c++
sushamaGavarskar1
 
PPT
inheritance
Amir_Mukhtar
 
inhertance c++
abhilashagupta
 
Inheritance
Tech_MX
 
Inheritance
Mustafa Khan
 
C# Inheritance
Prem Kumar Badri
 
Single inheritance
Äkshäý M S
 
Inheritance in c++theory
ProfSonaliGholveDoif
 
Inheritance OOP Concept in C++.
MASQ Technologies
 
Inheritance in c++ part1
Mirza Hussain
 
Inheritance in OOPS
Ronak Chhajed
 
Inheritance, Object Oriented Programming
Arslan Waseem
 
Inheritance in C++
RAJ KUMAR
 
Inheritance
Ashish Awasthi
 
Inheritance In C++ (Object Oriented Programming)
Gajendra Singh Thakur
 
inheritance c++
Muraleedhar Sundararajan
 
Friend functions
Megha Singh
 
Inheritance in oops
Hirra Sultan
 
Inheritance in c++
Paumil Patel
 
Advance OOP concepts in Python
Sujith Kumar
 
00ps inheritace using c++
sushamaGavarskar1
 
inheritance
Amir_Mukhtar
 

Similar to Inheritance (20)

PPT
Inheritance
abhay singh
 
PPTX
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
PPTX
Inheritance.pptx
PRIYACHAURASIYA25
 
PPTX
Object oriented programming Fundamental Concepts
Bharat Kalia
 
PDF
lecture 6.pdf
WaqarRaj1
 
PPT
11 Inheritance.ppt
LadallaRajKumar
 
PPTX
Inheritance
sourav verma
 
PPT
20 Object-oriented programming principles
maznabili
 
PPT
Inheritance in C++
Shweta Shah
 
PPTX
OOPS.pptx
NitinSharma134320
 
PPT
10 - Encapsulation(object oriented programming)- java . ppt
VhlRddy
 
PPT
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
PPT
6 Inheritance
Praveen M Jigajinni
 
PDF
UNIT 4.pdf.........................................
nikhithanikky1212
 
PPT
Introduction to Python - Part Three
amiable_indian
 
PDF
Inheritance
Pranali Chaudhari
 
PPTX
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
PPTX
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
DOCX
UNIT 4.docx.........................................
nikhithanikky1212
 
PPT
Inheritance in java.ppt
SeethaDinesh
 
Inheritance
abhay singh
 
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
Inheritance.pptx
PRIYACHAURASIYA25
 
Object oriented programming Fundamental Concepts
Bharat Kalia
 
lecture 6.pdf
WaqarRaj1
 
11 Inheritance.ppt
LadallaRajKumar
 
Inheritance
sourav verma
 
20 Object-oriented programming principles
maznabili
 
Inheritance in C++
Shweta Shah
 
10 - Encapsulation(object oriented programming)- java . ppt
VhlRddy
 
20. Object-Oriented Programming Fundamental Principles
Intro C# Book
 
6 Inheritance
Praveen M Jigajinni
 
UNIT 4.pdf.........................................
nikhithanikky1212
 
Introduction to Python - Part Three
amiable_indian
 
Inheritance
Pranali Chaudhari
 
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
yazad dumasia
 
UNIT 4.docx.........................................
nikhithanikky1212
 
Inheritance in java.ppt
SeethaDinesh
 
Ad

Recently uploaded (20)

PDF
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PDF
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
PPTX
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PDF
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
PPTX
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
PDF
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
DOCX
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PPTX
Different types of inheritance in odoo 18
Celine George
 
PDF
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
PPTX
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PPTX
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
PDF
Introduction presentation of the patentbutler tool
MIPLM
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Governor Josh Stein letter to NC delegation of U.S. House
Mebane Rash
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Horarios de distribución de agua en julio
pegazohn1978
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Lean IP - Lecture by Dr Oliver Baldus at the MIPLM 2025
MIPLM
 
Building Powerful Agentic AI with Google ADK, MCP, RAG, and Ollama.pptx
Tamanna36
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Lesson 1 - Nature of Inquiry and Research.pdf
marvinnbustamante1
 
How to Create a Customer From Website in Odoo 18.pptx
Celine George
 
WATERSHED MANAGEMENT CASE STUDIES - ULUGURU MOUNTAINS AND ARVARI RIVERpdf
Ar.Asna
 
Lesson 1 - Nature and Inquiry of Research
marvinnbustamante1
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Different types of inheritance in odoo 18
Celine George
 
I3PM Industry Case Study Siemens on Strategic and Value-Oriented IP Management
MIPLM
 
Light Reflection and Refraction- Activities - Class X Science
SONU ACADEMY
 
infertility, types,causes, impact, and management
Ritu480198
 
ENG8_Q1_WEEK2_LESSON1. Presentation pptx
marawehsvinetshe
 
Introduction presentation of the patentbutler tool
MIPLM
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Is Assignment Help Legal in Australia_.pdf
thomas19williams83
 
Ad

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()