Python Interfaces

An abstract class can contain both abstract and non-abstract methods if an abstract class contain only abstract method, then the class is called an interface.

A 100% pure abstract class is nothing but an interface. An interface acts as a service requirement specification.

Example:

class Test(ABC):
         @absractmethod
         def method1(self):
                pass
        @absractmethod
        def method2(self):
                pass

The Purpose of Interface

An abstract class containing only abstract method acts as requirement specification, anyone can provide an implementation in their way, and hence a single interface contain multiple applications in their form.

The following example demonstrates the purpose of the interface.

from abc import *
class CollegeAutomation(ABC):    ##requirement apecification
    @abstractmethod
    def method1(self):pass
    @abstractmethod
    def method2(self):pass
    @abstractmethod
    def method3(self):pass

class AaruSoftImpl(CollegeAutomation):
    def method1(self):
        print("Method1 implementation")
        
    def method2(self):
        print("Method2 implementation")
        
    def method3(self):
        print("Method3 implementation")
        
d=AaruSoftImpl()  ##creating an object and calling method1, method2 and method3
d.method1()
d.method2()
d.method3()

The output is:

interface-demo-program

The difference between Interface vs Abstract class vs Concrete class

Interface Abstract class Concrete class
If we do not know anything about implementation and we want to know requirement specification, then we should go for Interface If we are talking about implementation but not wholly, then we should go for abstract class If we are talking about complete implementation and ready to provide service, then we should go for a concrete class

The following example demonstrates the difference between interface, abstract class, and concrete class.

from abc import *
class CollegeAutomation(ABC):  ##Interfcae, because it contains only abstract methods
    @abstractmethod
    def method1(self):pass
    @abstractmethod
    def method2(self):pass
    @abstractmethod
    def method3(self):pass
/*Abstract class because it provides implementation for only method1 and method2 and not for method3*/
class AbsClass(CollegeAutomation):  
    def method1(self):
        print("Method1 implementation")
        
    def method2(self):
        print("Method2 implementation")
/*Concrete class because it calls internally method1 and method2 and also providing implementation to method3 */     
class ConcreteClass(AbsClass): 
    def method3(self):
        print("Method3 implementation")
        
c=ConcreteClass()    #creating an object 
c.method1()
c.method2()
c.method3()

The output is:

difference-between-interface-abstract-conacrete-methods

Python Input and Output Statements

Public, Private and Protected members access Specifiers

Public Members:

If a member(Either Method or Variable) is public, then we can access that member from anywhere either within the class or from outside of the class.

By default, every member present in the python class is public; hence, we can access from anywhere either within the class or from outside of the class.

The following example demonstrates the Public member access specifier.

class Test:
    def __init__(self):
        self.x=10
        
    def method1(Self):      ##public method
        print("It is public Method")
        
    def method2(self):
        print(self.x)
        self.method1()
        

print(t.x)  ##accessing public member
t.method1()      ##accessing public method outside of the class

The output is:

accessing-public-member

Private Members:

If a member is private, we can access only within the class and not from outside the class.

We can declare a member as private explicitly by prefixing it with two underscore symbols.

__x=10    ##declaring private variable/member
def  __method1(self)  ##declaring private method

The following example demonstrates the private members

class Test:
    def __init__(self):
        self.__x=10    ##declaring private member explicitly
        
    def __method1(Self):   ####declaring private method explicitly
        print("It is Private Method")
        
    def method2(self):
        print(self.__x)  ##accessing the private member within the class
        self.__method1()   ##accessing the private method within the class
        
t=Test()
t.method2()      

The output is:

accessing-the-private-members

If you want to call the individual members outside the class, then python will be thrown an attribute error.

class Test:
    def __init__(self):
        self.__x=10
        
    def __method1(Self):
        print("It is Private Method")
        
    def method2(self):
        print(self.__x)
        self.__method1()
        
t=Test()
t.method2()     
print(t.__x)  ##calling private members outside the class
t.__method1 ##calling private members outside the class

The output is:

accessing-private-members-outside-the-class

Name Mangling

Accessing the private members from outside the class by using the name mangling, the name mangling will change the name of the private variable to a new name, and hence we can access the private members from outside the class.

Example:

__VariableName==>_ClassName__VariableName

The syntax to perform Name mangling is:

print(objectreference._ClassName__VariableName)
This space will be replaced Automatically with ads, so do not delete me. Must be inserted only Above H3, H4, H5

Protected Members:

Protected members are the members, we can access anywhere within the class, but from outside of the class we access only from the child class.

We can declare a protected member explicitly by using the single underscore symbol

_x=10

The following example demonstrates the protected member

class Test:
    def __init__(self):
        self._x=10 
        
    def method1(self):
        print(self._x)
        
class SubTest(Test):
    def method2(self):
        print(self._x)
      
        
t=SubTest()
t.method1()  
t.method2() 

The output is:

accessing-protected-members

But, the protected members are just for the naming convention but not implemented theoretically in python, and hence you can access the protected member even out of the class also.

class Test:
    def __init__(self):
        self._x=10 
        
    def method1(self):
        print(self._x)
        
class SubTest(Test):
    def method2(self):
        print(self._x)
      
        
t=SubTest()
t.method1()  
t.method2()   
print(t._x)   ##accessing the protected member outside the class

The output is:

accessing-the-protected-member-outside-the-class

Python Program Flow Control

Data Hiding in python

Data Hiding is one of the important features of object-oriented programming language, which allows preventing the functions of the program to access the internal representation of a class type directly.

That means our internal data should not go out directly. By declaring the data members as private, we can implement data hiding.

The following example demonstrates accessing the bank balance directly

class Account:
    def __init__(self,initial_balance):
        self.balance=initial_balance
        
a=Account(10000)
print(a.balance)        

##The output is:
10000

In the above programming, we are accessing the bank balance directly, this may cause fraud actions, and anyone can easily access your bank account details, to prevent this we can perform data hiding by implementing it.

By declaring the balance as a private member and when we try to access this balance outside the class, then python will throw an attribute error because we cannot access private members outside the class.

class Account:
    def __init__(self,initial_balance):
        self.__balance=initial_balance  ##declaring balance as private member   
        
a=Account(10000)
print(a.__balance)     ##accessing the balance outside the class

The output is:

accesing-the-private-member-outside-the-class

So, we cannot access the data outside the class and hence call a method, and through that method, we obtain the data.

class Account:
    def __init__(self,initial_balance):
        self.__balance=initial_balance
        
    def getBalance(self): ##accessing the data by using method
        ##validation or autentication
        return self.__balance
        
a=Account(10000)
#print(a.__balance)
print(a.getBalance())        ##calling a method


##The output is:
10000

Super() Function in python

Encapsulation in python

The process of Binding/grouping/encapsulating data and corresponding behavior into a single unit is called as Encapsulating.

Example: Every python class is an example of encapsulation.

Any component that follows Data hiding and abstraction, such type of component is said to be encapsulated part.

Encapsulating=DataHiding+Abstraction

Example:

class Account:
    def __init__(self,initial_balance):   
        self.__balance=initial_balance   ##declaring private member
        
    def getBalance(self):    ##abstraction methods
        ##validation or autentication
        return self.__balance
    def deposite(self.amount):
        self.__balance=self.__balance+amount
        
    def Withdraw(self,amount):
        self.__balance=self.__balance-amount
Hiding the data behind methods is the main concept of encapsulation

The Advantages of Encapsulation are:

  • Security
  • The enhancement will become very easy
  • The maintainability and modularity will be improved

The Limitation of Encapsulation is:

Encapsulation increases the length of the code and slows down the execution, and hence we should compromise with performance.

If we want security, we should compromise with performance, and if we want performance, we should compromise with security.

The Three Pillars of Python OOP's concept

  • Inheritance
  • Polymorphism
  • Encapsulation
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions