SlideShare a Scribd company logo
3
Introduction
Example:
To understand that Myclass method is shared by all objects
class Myclass:
def calculate(self, x):
print("Square: ", x * x)
#All objects share same calculate() method
obj1 = Myclass()
obj1.calculate(2)
obj2 = Myclass()
obj2.calculate(3)
obj3 = Myclass()
obj3.calculate(4)
Most read
8
Abstract Method & Class

Abstract Method

- Is the method whose action is redefined in sub classes as per the requirements

of the objects

- Use decorator @abstractmethod to mark it as abstract method

- Are written without body

Abstract Class

- Is a class generally contains some abstract methods

- PVM cannot create objects to abstract class, since memory needed will not be

known in advance

- Since all abstract classes should be derived from the meta class ABC which belongs to
abc(abstract base class) module, we need to import this module

- To import abstract class, use

- from abc import ABC, abstractmethod

OR

- from abc import *
Most read
13
Interfaces

Abstract classes contains both,

- Abstract methods

- Concrete Methods

Interfaces is also an Abstract class, but contains only

- Abstract methods

Plus point of Interface.

- Every sub-class may provide its own implementation for the abstract methods
Most read
Abstract Classes And
Interfaces
Team Emertxe
Introduction
Introduction
Example:
To understand that Myclass method is shared by all objects
class Myclass:
def calculate(self, x):
print("Square: ", x * x)
#All objects share same calculate() method
obj1 = Myclass()
obj1.calculate(2)
obj2 = Myclass()
obj2.calculate(3)
obj3 = Myclass()
obj3.calculate(4)
Question

What If?

Object-1 wants to calculate square value

Object-2 wants to calculate square root

Object-3 wants to calculate Cube
Solution-1

Define, three methods in the same class

calculate_square()

calculate_sqrt()

calculate_cube()

Disadvantage:

All three methods are available to all the objects which is not advisable
Solution-2
calculate(x):
no body
calculate(x):
square of x
calculate(x):
sqrt of x
calculate(x):
cube of x
Obj1 Obj2 Obj3
Myclass
Sub1 Sub2 Sub3
Abstract Method and Class
Abstract Method & Class

Abstract Method

- Is the method whose action is redefined in sub classes as per the requirements

of the objects

- Use decorator @abstractmethod to mark it as abstract method

- Are written without body

Abstract Class

- Is a class generally contains some abstract methods

- PVM cannot create objects to abstract class, since memory needed will not be

known in advance

- Since all abstract classes should be derived from the meta class ABC which belongs to
abc(abstract base class) module, we need to import this module

- To import abstract class, use

- from abc import ABC, abstractmethod

OR

- from abc import *
Program-1
#To create abstract class and sub classes which implement the abstract method of the
abstract class
from abc import ABC, abstractmethod
class Myclass(ABC):
@abstractmethod
def calculate(self, x):
pass
#Sub class-1
class Sub1(Myclass):
def calculate(self, x):
print("Square: ", x * x)
Obj1 = Sub1()
Obj1.calculate(2)
Obj2 = Sub2()
Obj2.calculate(16)
Obj3 = Sub3()
Obj2.calculate(3)
#Sub class-2
import math
class Sub2(Myclass):
def calculate(self, x):
print("Square root: ", math.sqrt(x))
#Sub class-3
class Sub3(Myclass):
def calculate(self, x):
print("Cube: ", x * x * x)
Example-2

Maruthi, Santro, Benz are all objects of class Car
Registration no. - All cars will have reg. no.
- Create var for it
Fuel Tank - All cars will have common fule tank
- Action: Open, Fill, Close
Steering - All cars will not have common steering
say, Maruthi uses- Manual steering
Santro uses - Power steering
- So define this as an Abstract Method
Brakes - Maruthi uses hydraulic brakes
- Santro uses gas brakes
- So define this as an Abstract Method
Program-2
#Define an absract class
from abc import *
class Car(ABC):
def __init__(self, reg_no):
self.reg_no = reg_no
def opentank(self):
print("Fill the fuel for car with reg_no: ",
self.reg_no)
@abstractmethod
def steering(self):
pass
@abstractmethod
def braking(self):
pass
#Define the Maruthi class
from abstract import Car
class Maruthi(Car):
def steering(self):
print("Maruthi uses Manual steering")
def braking(self):
print("Maruthi uses hydraulic braking system")
#Create the objects
Obj = Maruthi(123)
Obj.opentank()
Obj.steering()
Obj.braking()
Interfaces
Interfaces

Abstract classes contains both,

- Abstract methods

- Concrete Methods

Interfaces is also an Abstract class, but contains only

- Abstract methods

Plus point of Interface.

- Every sub-class may provide its own implementation for the abstract methods
Interfaces
Program-1
from abc import *
class Myclass(ABC):
@abstractmethod
def connect(self):
pass
@abstractmethod
def disconnect(self):
pass
#Define Database
class Database:
str = input("Enter the database name: ")
#Covert the string into the class name
classname = globals()[str]
#create an object
x = classname()
#Call methods
x.connect()
x.disconnect()
#Sub-Class:1
class Oracle(Myclass):
def connect(self):
print("Connecting to oracle database...")
def disconnect(self):
print("Disconnecting from oracle
database...")
#Sub-Class:2
class Sybase(Myclass):
def connect(self):
print("Connecting to sybase database...")
def disconnect(self):
print("Disconnecting from sybase
database...")
Interfaces
Program-2
from abc import *
class Myclass(ABC):
@abstractmethod
def putdata(self, text):
pass
@abstractmethod
def disconnect(self):
pass
#Define Printer
class Printer:
str = input("Enter the printer name: ")
#Covert the string into the class name
classname = globals()[str]
#create an object
x = classname()
#Call methods
x.putdata("Sending to printer")
x.disconnect()
#Sub-Class:1
class IBM(Myclass):
def putdata(self, text):
print(text)
def disconnect(self):
print("Disconnecting from IBM printer...")
#Sub-Class:2
class Epson(Myclass):
def putdata(self, text):
print(text)
def disconnect(self):
print("Disconnecting from Epson printer...")
THANK YOU

More Related Content

What's hot (20)

Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Python programming : Classes objects
Python programming : Classes objectsPython programming : Classes objects
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Python-Classes.pptx
Python-Classes.pptxPython-Classes.pptx
Python-Classes.pptx
Karudaiyar Ganapathy
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
Praveen M Jigajinni
 
Python-Encapsulation.pptx
Python-Encapsulation.pptxPython-Encapsulation.pptx
Python-Encapsulation.pptx
Karudaiyar Ganapathy
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
Thooyavan Venkatachalam
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
Arslan Arshad
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Python-DataAbstarction.pptx
Python-DataAbstarction.pptxPython-DataAbstarction.pptx
Python-DataAbstarction.pptx
Karudaiyar Ganapathy
 
Python-Inheritance.pptx
Python-Inheritance.pptxPython-Inheritance.pptx
Python-Inheritance.pptx
Karudaiyar Ganapathy
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Python Flow Control
Python Flow ControlPython Flow Control
Python Flow Control
Mohammed Sikander
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Python : Data Types
Python : Data TypesPython : Data Types
Python : Data Types
Emertxe Information Technologies Pvt Ltd
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
Vinod Kumar
 
Oops ppt
Oops pptOops ppt
Oops ppt
abhayjuneja
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
RAGAVIC2
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Modules and packages in python
Modules and packages in pythonModules and packages in python
Modules and packages in python
TMARAGATHAM
 
Object oriented programming with python
Object oriented programming with pythonObject oriented programming with python
Object oriented programming with python
Arslan Arshad
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
Sujan Mia
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Oops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in JavaOops concepts || Object Oriented Programming Concepts in Java
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
RAGAVIC2
 
Class, object and inheritance in python
Class, object and inheritance in pythonClass, object and inheritance in python
Class, object and inheritance in python
Santosh Verma
 

Similar to Python programming : Abstract classes interfaces (20)

Lesson on Python Classes by Matt Wufus 2003
Lesson on Python Classes by Matt Wufus 2003Lesson on Python Classes by Matt Wufus 2003
Lesson on Python Classes by Matt Wufus 2003
davidlin271898
 
Advanced Python, Part 1
Advanced Python, Part 1Advanced Python, Part 1
Advanced Python, Part 1
Zaar Hai
 
Inheritance
InheritanceInheritance
Inheritance
JayanthiNeelampalli
 
python slid share.pptx
python slid share.pptxpython slid share.pptx
python slid share.pptx
NagaVarthini
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
Python OOPs
Python OOPsPython OOPs
Python OOPs
Binay Kumar Ray
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
Abstraction1
Abstraction1Abstraction1
Abstraction1
zindadili
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
Haris Bin Zahid
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Python advance
Python advancePython advance
Python advance
Mukul Kirti Verma
 
Python3
Python3Python3
Python3
Ruchika Sinha
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
muneshwarbisen1
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptxPYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
mru761077
 
Lecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.pptLecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
Lecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.pptLecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
Class
ClassClass
Class
baabtra.com - No. 1 supplier of quality freshers
 
مقدمة بايثون .pptx
مقدمة بايثون .pptxمقدمة بايثون .pptx
مقدمة بايثون .pptx
AlmutasemBillahAlwas
 
Lesson on Python Classes by Matt Wufus 2003
Lesson on Python Classes by Matt Wufus 2003Lesson on Python Classes by Matt Wufus 2003
Lesson on Python Classes by Matt Wufus 2003
davidlin271898
 
Advanced Python, Part 1
Advanced Python, Part 1Advanced Python, Part 1
Advanced Python, Part 1
Zaar Hai
 
python slid share.pptx
python slid share.pptxpython slid share.pptx
python slid share.pptx
NagaVarthini
 
Object Oriented Programming.pptx
Object Oriented Programming.pptxObject Oriented Programming.pptx
Object Oriented Programming.pptx
SAICHARANREDDYN
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
Mohammad Reza Kamalifard
 
Abstraction1
Abstraction1Abstraction1
Abstraction1
zindadili
 
Abstract class and Interface
Abstract class and InterfaceAbstract class and Interface
Abstract class and Interface
Haris Bin Zahid
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Mohammad Reza Kamalifard
 
Chap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.pptChap 3 Python Object Oriented Programming - Copy.ppt
Chap 3 Python Object Oriented Programming - Copy.ppt
muneshwarbisen1
 
Introduction to Python - Part Three
Introduction to Python - Part ThreeIntroduction to Python - Part Three
Introduction to Python - Part Three
amiable_indian
 
Basic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptxBasic_concepts_of_OOPS_in_Python.pptx
Basic_concepts_of_OOPS_in_Python.pptx
santoshkumar811204
 
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptxPYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
PYTHON-COURSE-PROGRAMMING-UNIT-IV--.pptx
mru761077
 
Lecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.pptLecture topic - Python class lecture.ppt
Lecture topic - Python class lecture.ppt
Reji K Dhaman
 
Lecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.pptLecture on Python class -lecture123456.ppt
Lecture on Python class -lecture123456.ppt
Reji K Dhaman
 
Ad

More from Emertxe Information Technologies Pvt Ltd (20)

Career Transition (1).pdf
Career Transition (1).pdfCareer Transition (1).pdf
Career Transition (1).pdf
Emertxe Information Technologies Pvt Ltd
 
10_isxdigit.pdf
10_isxdigit.pdf10_isxdigit.pdf
10_isxdigit.pdf
Emertxe Information Technologies Pvt Ltd
 
01_student_record.pdf
01_student_record.pdf01_student_record.pdf
01_student_record.pdf
Emertxe Information Technologies Pvt Ltd
 
02_swap.pdf
02_swap.pdf02_swap.pdf
02_swap.pdf
Emertxe Information Technologies Pvt Ltd
 
01_sizeof.pdf
01_sizeof.pdf01_sizeof.pdf
01_sizeof.pdf
Emertxe Information Technologies Pvt Ltd
 
07_product_matrix.pdf
07_product_matrix.pdf07_product_matrix.pdf
07_product_matrix.pdf
Emertxe Information Technologies Pvt Ltd
 
06_sort_names.pdf
06_sort_names.pdf06_sort_names.pdf
06_sort_names.pdf
Emertxe Information Technologies Pvt Ltd
 
05_fragments.pdf
05_fragments.pdf05_fragments.pdf
05_fragments.pdf
Emertxe Information Technologies Pvt Ltd
 
04_magic_square.pdf
04_magic_square.pdf04_magic_square.pdf
04_magic_square.pdf
Emertxe Information Technologies Pvt Ltd
 
03_endianess.pdf
03_endianess.pdf03_endianess.pdf
03_endianess.pdf
Emertxe Information Technologies Pvt Ltd
 
02_variance.pdf
02_variance.pdf02_variance.pdf
02_variance.pdf
Emertxe Information Technologies Pvt Ltd
 
01_memory_manager.pdf
01_memory_manager.pdf01_memory_manager.pdf
01_memory_manager.pdf
Emertxe Information Technologies Pvt Ltd
 
09_nrps.pdf
09_nrps.pdf09_nrps.pdf
09_nrps.pdf
Emertxe Information Technologies Pvt Ltd
 
11_pangram.pdf
11_pangram.pdf11_pangram.pdf
11_pangram.pdf
Emertxe Information Technologies Pvt Ltd
 
10_combinations.pdf
10_combinations.pdf10_combinations.pdf
10_combinations.pdf
Emertxe Information Technologies Pvt Ltd
 
08_squeeze.pdf
08_squeeze.pdf08_squeeze.pdf
08_squeeze.pdf
Emertxe Information Technologies Pvt Ltd
 
07_strtok.pdf
07_strtok.pdf07_strtok.pdf
07_strtok.pdf
Emertxe Information Technologies Pvt Ltd
 
06_reverserec.pdf
06_reverserec.pdf06_reverserec.pdf
06_reverserec.pdf
Emertxe Information Technologies Pvt Ltd
 
05_reverseiter.pdf
05_reverseiter.pdf05_reverseiter.pdf
05_reverseiter.pdf
Emertxe Information Technologies Pvt Ltd
 
Ad

Recently uploaded (20)

TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy SurveyTrustArc Webinar - 2025 Global Privacy Survey
TrustArc Webinar - 2025 Global Privacy Survey
TrustArc
 
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdfvertical-cnc-processing-centers-drillteq-v-200-en.pdf
vertical-cnc-processing-centers-drillteq-v-200-en.pdf
AmirStern2
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025TimeSeries Machine Learning - PyData London 2025
TimeSeries Machine Learning - PyData London 2025
Suyash Joshi
 
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdfHow Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
How Advanced Environmental Detection Is Revolutionizing Oil & Gas Safety.pdf
Rejig Digital
 
Data Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any ApplicationData Virtualization: Bringing the Power of FME to Any Application
Data Virtualization: Bringing the Power of FME to Any Application
Safe Software
 
DevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical PodcastDevOps in the Modern Era - Thoughtfully Critical Podcast
DevOps in the Modern Era - Thoughtfully Critical Podcast
Chris Wahl
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementaryMurdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
ELNL2025 - Unlocking the Power of Sensitivity Labels - A Comprehensive Guide....
Jasper Oosterveld
 
Jira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : IntroductionJira Administration Training – Day 1 : Introduction
Jira Administration Training – Day 1 : Introduction
Ravi Teja
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Domino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use CasesDomino IQ – What to Expect, First Steps and Use Cases
Domino IQ – What to Expect, First Steps and Use Cases
panagenda
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training RoadblocksDown the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
LSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection FunctionLSNIF: Locally-Subdivided Neural Intersection Function
LSNIF: Locally-Subdivided Neural Intersection Function
Takahiro Harada
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
National Fuels Treatments Initiative: Building a Seamless Map of Hazardous Fu...
Safe Software
 
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und AnwendungsfälleDomino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
Domino IQ – Was Sie erwartet, erste Schritte und Anwendungsfälle
panagenda
 
Oracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI FoundationsOracle Cloud Infrastructure AI Foundations
Oracle Cloud Infrastructure AI Foundations
VICTOR MAESTRE RAMIREZ
 

Python programming : Abstract classes interfaces

  • 3. Introduction Example: To understand that Myclass method is shared by all objects class Myclass: def calculate(self, x): print("Square: ", x * x) #All objects share same calculate() method obj1 = Myclass() obj1.calculate(2) obj2 = Myclass() obj2.calculate(3) obj3 = Myclass() obj3.calculate(4)
  • 4. Question  What If?  Object-1 wants to calculate square value  Object-2 wants to calculate square root  Object-3 wants to calculate Cube
  • 5. Solution-1  Define, three methods in the same class  calculate_square()  calculate_sqrt()  calculate_cube()  Disadvantage:  All three methods are available to all the objects which is not advisable
  • 6. Solution-2 calculate(x): no body calculate(x): square of x calculate(x): sqrt of x calculate(x): cube of x Obj1 Obj2 Obj3 Myclass Sub1 Sub2 Sub3
  • 8. Abstract Method & Class  Abstract Method  - Is the method whose action is redefined in sub classes as per the requirements  of the objects  - Use decorator @abstractmethod to mark it as abstract method  - Are written without body  Abstract Class  - Is a class generally contains some abstract methods  - PVM cannot create objects to abstract class, since memory needed will not be  known in advance  - Since all abstract classes should be derived from the meta class ABC which belongs to abc(abstract base class) module, we need to import this module  - To import abstract class, use  - from abc import ABC, abstractmethod  OR  - from abc import *
  • 9. Program-1 #To create abstract class and sub classes which implement the abstract method of the abstract class from abc import ABC, abstractmethod class Myclass(ABC): @abstractmethod def calculate(self, x): pass #Sub class-1 class Sub1(Myclass): def calculate(self, x): print("Square: ", x * x) Obj1 = Sub1() Obj1.calculate(2) Obj2 = Sub2() Obj2.calculate(16) Obj3 = Sub3() Obj2.calculate(3) #Sub class-2 import math class Sub2(Myclass): def calculate(self, x): print("Square root: ", math.sqrt(x)) #Sub class-3 class Sub3(Myclass): def calculate(self, x): print("Cube: ", x * x * x)
  • 10. Example-2  Maruthi, Santro, Benz are all objects of class Car Registration no. - All cars will have reg. no. - Create var for it Fuel Tank - All cars will have common fule tank - Action: Open, Fill, Close Steering - All cars will not have common steering say, Maruthi uses- Manual steering Santro uses - Power steering - So define this as an Abstract Method Brakes - Maruthi uses hydraulic brakes - Santro uses gas brakes - So define this as an Abstract Method
  • 11. Program-2 #Define an absract class from abc import * class Car(ABC): def __init__(self, reg_no): self.reg_no = reg_no def opentank(self): print("Fill the fuel for car with reg_no: ", self.reg_no) @abstractmethod def steering(self): pass @abstractmethod def braking(self): pass #Define the Maruthi class from abstract import Car class Maruthi(Car): def steering(self): print("Maruthi uses Manual steering") def braking(self): print("Maruthi uses hydraulic braking system") #Create the objects Obj = Maruthi(123) Obj.opentank() Obj.steering() Obj.braking()
  • 13. Interfaces  Abstract classes contains both,  - Abstract methods  - Concrete Methods  Interfaces is also an Abstract class, but contains only  - Abstract methods  Plus point of Interface.  - Every sub-class may provide its own implementation for the abstract methods
  • 14. Interfaces Program-1 from abc import * class Myclass(ABC): @abstractmethod def connect(self): pass @abstractmethod def disconnect(self): pass #Define Database class Database: str = input("Enter the database name: ") #Covert the string into the class name classname = globals()[str] #create an object x = classname() #Call methods x.connect() x.disconnect() #Sub-Class:1 class Oracle(Myclass): def connect(self): print("Connecting to oracle database...") def disconnect(self): print("Disconnecting from oracle database...") #Sub-Class:2 class Sybase(Myclass): def connect(self): print("Connecting to sybase database...") def disconnect(self): print("Disconnecting from sybase database...")
  • 15. Interfaces Program-2 from abc import * class Myclass(ABC): @abstractmethod def putdata(self, text): pass @abstractmethod def disconnect(self): pass #Define Printer class Printer: str = input("Enter the printer name: ") #Covert the string into the class name classname = globals()[str] #create an object x = classname() #Call methods x.putdata("Sending to printer") x.disconnect() #Sub-Class:1 class IBM(Myclass): def putdata(self, text): print(text) def disconnect(self): print("Disconnecting from IBM printer...") #Sub-Class:2 class Epson(Myclass): def putdata(self, text): print(text) def disconnect(self): print("Disconnecting from Epson printer...")