Interface in Python


 Interface

In Python, the interface concept is not explicitly available, like available in other language like java. 

In Python, an interface is an abstract class which contains only abstract methods but not a single concrete (normal) method.

Rules for interface

  • All methods of an interface is abstract.
  • We can not create object of interface.
  • If a class is implementing an interface it has to define all the methods gives in that interface.
  • If a class does not implement all the methods declared in the interface, the class must be declared abstract.

Creating Interface

from abc import ABC, abstractmethod

class Father(ABC):

        @abstractmethod

        def display(self):

                        pass

We use interface when all the features need to be implemented differently for different object.

Example

from abc import ABC,abstractmethod

class Father(ABC):

        @abstractmethod

            def display(self):

                        pass

class Child(Father):

            def display(self):

                        print("Welcome to technicalguptaji tutorial")

                        print("declaring interface")

c=Child()

c.display()

Output

Welcome to technicalguptaji tutorial

declaring interface











No comments:

Post a Comment