What is Abstraction | introduction of abstraction with programs |


what is Abstraction | introduction of abstraction with programs |



Introduction of Abstraction –


Abstraction means hiding the complexity and only showing the essential features of the object or application. So in a way abstraction mean hiding the real implementation and we as a user, knowing only how to use it.Data abstraction is programming technique that relies on separation of interface and implementation .

Real life example : - TV , vehicle etc 

TV is example which you do not know its internal details but only know turn off and on , change the channel ,adjust the volume and add external components such as speakers, DVD etc . 
i.e Abstraction providing only essential information and hiding their background details ( it is represent information in program without presenting the details.)



what is Abstraction | introduction of abstraction with programs |
Abstraction in python 





Abstraction in Python –

Abstraction in python is achieved by using Abstract Classes and Interfaces.
Abstract class is a class that generally provides incomplete functionality and contains one or more abstract methods.
Abstract Method are the method that generally don’t have any implementation it is left to the Sub Classes to provide implementation for abstract methods

Abstract Class in Python –
In Python class is created by deriving from the meta class ABC which belongs to the abc (Abstract Base Class) Modules 

Syntax for Creating Abstract Class in Python –
From abc import ABC
class Class_name(ABC):


From abc module, ABC class has to be imported and abstract class has to inherit ABC class in order to be considered as an abstract class 

Abstract Method in Python –

For defining abstract  method in an abstract class method has to be decorated with @abstractmethod decorator .

Syntax  - 
defining ABC method in abstract class in python is as given below


From abc import ABC , abstractmethod
Class Class_name (ABC):
@abstractmethod
def mymethod(self):
# empty body
Pass

Program -

from abc import ABC, abstractmethod 
class Animal(ABC):      # abstract class 
    @abstractmethod      
    def eat(self):            # abstract method 
        pass
  
class Tiger(Animal): 
  
    def eat(self): 
        print("eat Non-Veg") 
  
class Cow(Animal): 
  
    def eat(self): 
        print("eat Veg only") 
  

t= Tiger() 
t.eat() 
c=Cow() 
c.eat() 

Output


eat Non-Veg
eat Veg only

Post a Comment

Post a Comment (0)

Previous Post Next Post