IKH

Abstract Method And Abstract Class

Abstract Method

  • Sometimes we don’t know about implementation, still we can declare a method. Such type of methods are called abstract methods. i.e abstract method has only declaration but not implementation.
  • In python we can declare abstract method by using @abstractmethod decorator as follows.
  • @abstractmethod def m1(self): pass
  • @abstractmethod decorator present in abc module. Hence compulsory we should import abc module, otherwise we will get error. abc==>abstract base class module
  • class Test:
  • @abstractmethod
  • def m1(self):
  • pass

NameError: name ‘abstractmethod’ is not defined.

Example

Python
from abc import * 
class Test: 
@abstractmethod 
def m1(self): 
pass 

Output

PowerShell
7

Example

Python
from abc import * 
class Fruit: 
@abstractmethod 
def taste(self): 
pass 
Child classes are responsible to provide implemention for parent class abstract methods.

Output

PowerShell
5

Abstract class

  • Some times implementation of a class is not complete, such type of partially implementation classes are called abstract classes. Every abstract class in Python should be derived from ABC class which is present in abc module.

Case-1

Python
from abc import *
class Test:
pass

t=Test()
In the above code we can create object for Test class b'z it is concrete class and it does not conatin any abstract method.

Case-2

Python
from abc import *
class Test(ABC):
pass

t=Test()
In the above code we can create object,even it is derived from ABC class,b'z it does not contain any abstract method.

Case-3

Python
from abc import *
class Test(ABC):
@abstractmethod
def m1(self):
pass

t=Test()
TypeError: Can't instantiate abstract class Test with abstract methods m1

Case-4

Python
from abc import *
class Test:
@abstractmethod
def m1(self):
pass

t=Test()
We can create object even class contains abstract method b'z we are not extending ABC class.

Case-5

Python
from abc import *
class Test:
@abstractmethod
def m1(self):
print('Hello')

t=Test()
t.m1()

Output

PowerShell
Hello
Conclusion: If a class contains atleast one abstract method and if we are extending ABC class then instantiation is not possible.
"abstract class with abstract method instantiation is not possible"
Parent class abstract methods should be implemented in the child classes. otherwise we cannot instantiate child class. If we are not creating child class object then we won't get any error.

Case-1

Python
from abc import *
class Vehicle(ABC)
@abstractmethod
def noofwheels(self)
pass

class Bus(Vehicle): pass
It is valid b'z we are not creating Child class object.

Case-2

Python
from abc import *
class Vehicle(ABC):
@abstractmethod
def noofwheels(self):
pass

 class Bus(Vehicle): pass
b=Bus()
TypeError: Can't instantiate abstract class Bus with abstract methods noofwheels

Note

  • If we are extending abstract class and does not override its abstract method then child class is also abstract and instantiation is not possible.

Example

Python
from abc import * 
class Vehicle(ABC): 
@abstractmethod 
def noofwheels(self): 
pass 
 
class Bus(Vehicle): 
def noofwheels(self): 
return 7 
 
class Auto(Vehicle): 
def noofwheels(self): 
return 3 
b=Bus() 
print(b.noofwheels())#7 
 
a=Auto() 
print(a.noofwheels())#3 

Output

PowerShell
5

Note

  • Abstract class can contain both abstract and non-abstract methods also.

Interfaces

  • In general if an abstract class contains only abstract methods such type of abstract class is considered as interface.

Demo program

Python
from abc import *
class DBInterface(ABC):
@abstractmethod
def connect(self):pass

@abstractmethod
def disconnect(self):pass

class Oracle(DBInterface):
def connect(self):
print('Connecting to Oracle Database…')
def disconnect(self):
print('Disconnecting to Oracle Database…')

class Sybase(DBInterface):
def connect(self):
print('Connecting to Sybase Database…')
def disconnect(self):
print('Disconnecting to Sybase Database…')

dbname=input('Enter Database Name:')
classname=globals()[dbname]
x=classname()
x.connect()
x.disconnect()

Output

PowerShell
D:\durga_classes>py test.py
Enter Database Name:Oracle
Connecting to Oracle Database...
Disconnecting to Oracle Database...
D:\durga_classes>py test.py
Enter Database Name:Sybase
Connecting to Sybase Database...
Disconnecting to Sybase Database...

Note

  • The inbuilt function globals()[str] converts the string ‘str’ into a class name and returns the classname.

Demo program 2

  • Reading class name from the file

config.txt

  • EPSON

test.py

Python
from abc import *
class Printer(ABC):
@abstractmethod
def printit(self,text):pass

@abstractmethod
def disconnect(self):pass

class EPSON(Printer):
def printit(self,text):
print('Printing from EPSON Printer…')
print(text)
def disconnect(self):
print('Printing completed on EPSON Printer…')

class HP(Printer):
def printit(self,text):
print('Printing from HP Printer…')
print(text)
def disconnect(self):
print('Printing completed on HP Printer…')

with open('config.txt','r') as f:
pname=f.readline()

classname=globals()[pname]
x=classname()
x.printit('This data has to print…')
x.disconnect()

Output

PowerShell
Printing from EPSON Printer…
This data has to print…
Printing completed on EPSON Printer…

Concreate class vs Abstract Class vs Inteface

  • If we dont know anything about implementation just we have requirement specification then we should go for interface.
  • If we are talking about implementation but not completely then we should go for abstract class.(partially implemented class)
  • If we are talking about implementation completely and ready to provide service then we should go for concrete class.
Python
from abc import *
class CollegeAutomation(ABC):
@abstractmethod
def m1(self): pass
@abstractmethod
def m2(self): pass
@abstractmethod
def m3(self): pass
class AbsCls(CollegeAutomation):
def m1(self):
print('m1 method implementation')
def m2(self):
print('m2 method implementation')

class ConcreteCls(AbsCls):
def m3(self):
print('m3 method implemnentation')

c=ConcreteCls()
c.m1()
c.m2()
c.m3()

Report an error