Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods. Subclasses of an abstract cla...
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide implementations for the abstract methods. Subclasses of an abstract class in Python are not required to implement abstract methods of the parent class.
Size: 120.22 KB
Language: en
Added: May 09, 2019
Slides: 16 pages
Slide Content
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...")