ABSTRACTION
DEFINITION OF ABSTRACTION :
The process of highlighting set of services and hiding the implementations in child class. We can achieve abstraction using abstract class.
ABSTRACT CLASS:
We can declare normal class as abstract class by extending ABC class.
ABSTRACT METHOD:
Abstract method contains only method declaration without implementations. To declare abstract method we can use @abstract method qualifier.
EXAMPLE PROGRAMS:
Ex-1: import abc class A(ABC): @abstractmethod def m1(): pass class B(A): def m1(): print(“m1 method”) c=B() c.m()
We cannot create object for abstract class. To create object we have to write implementations for all abstract class methods.
Ex-2: import abc class A(ABC): @abstractmethod def add(self): pass @abstractmethod def sub(self): pass @abstractmethod def mul(self): pass class B(A): def add(self): print(10+20) def sub(self): print(20-10) class C(B): def mul(self): print(10*20) c=C() c.add() c.sub() c.mul()
Different classes can implement different implementations for same abstract method.
Ex-3: import abc class A(ABC): @abstractmethod def wish(self): pass class B(A): def wish(self): print(“good morning”) class C(A): def wish(self): print(“good evening”) b=B() b.wish() c=C() c.wish()
Ex-4: import abc class A(ABC): a=10 class B(A): def add(self,a): print(self.a+a) class C(A): def sub(self,a): print(self.a-a) b=B() b.add(20) c=C() c.sub(30)