Learn-Object_Oriented_programming-with-us

Learn By Topic:

          

Class-objects

          

Inheritance

          

Polymorphism

          

Encapsulation

          

Abstraction

          

          

Click here to toggle b/w normal and focus mode: FOCUS MODE

What is Abstraction ?

The process by which data and functions are defined in such a way that only essential details can be seen and unnecessary implementations are hidden is called Data Abstraction.

Abstraction is really powerful for making complex tasks and codes simpler when used in Object-Oriented Programming. It reduces the complexity for the user by making the relevant part accessible and usable leaving the unnecessary code hidden.

Example:

We use the TV remote to switch the TV ON or OFF, switch to different channels, and raise or lower the volume. The TV user only knows he/she may use the buttons on the remote to do it.But they don't know what happens internally , for example how the TV sensor is capturing signals from the TV remote etc. All the internal functionality is hidden, as for the user it might not be necessary for them to know how that is happening.

HOW ABSTRACT WORKS :

Python does not provide abstract classes. Python comes with a module that provides the base for defining Abstract Base classes(ABC) and that module name is ABC. ABC works by decorating methods of the base class as abstract and then registering concrete classes as implementations of the abstract base.

Example:-

from abc import ABC, abstractmethod

class Polygon(ABC):

    @abstractmethod
    def noofsides(self):
        pass

class Square(Polygon):

    # overriding abstract method
    def noofsides(self):
        print("I have 4 sides")

class Octagon(Polygon):

    # overriding abstract method
    def noofsides(self):
        print("I have 8 sides")

class Heptagon(Polygon):

    # overriding abstract method
    def noofsides(self):
        print("I have 7 sides")

class Hexagon(Polygon):

    # overriding abstract method
    def noofsides(self):
        print("I have 6 sides")

# Driver code
R = Square()
R.noofsides()

K = Hexagon()
K.noofsides()

R = Heptagon()
R.noofsides()

K = Octagon()
K.noofsides()

Output :

I have 4 sides
I have 6 sides
I have 7 sides
I have 8 sides

In the above code, we have imported the abc module to create the abstract base class.

We created different classes that inherited the ABC class and defined an abstract method named noofsides().

We have then inherited the base class from the three different subclasses and implemented the abstract method differently. We created the objects to call the abstract method.

Why use Abstract Base Class?

Defining an Abstract Base Class lets us create a common Application Programming Interface (API) for multiple subclasses. It is useful while working in large teams and code-bases so that all of the classes need not be remembered and also be provided as library by third parties.

Points to Remember

Below are the points which we should remember about the abstract base class in Python.

Abstraction is essential to hide the core functionality from the users. We have covered the all the basic concepts of Abstraction in Python.

Why Data Abstraction is Important?

Now that we know what Data Abstraction in Python is, we can also conclude how it is important.

Data Abstraction firstly saves a lot of our time as we do not have to repeat the code that may be the same for all the classes. Moreover, if there are any additional features, they can be easily added, thus improving flexibility. Not to mention, working in large teams becomes easier as one won’t have to remember every function and the basic structure can be inherited without any confusions.

Abstract Properties :  Abstract classes include attributes in addition to methods, you can require the attributes in concrete classes by defining them with @abstractproperty.

import abc
from abc import ABC, abstractmethod

class parent(ABC):
    @abc.abstractproperty
    def geeks(self):
        return "parent class"
class child(parent):

    @property
    def geeks(self):
        return "child class"


try:
    r =parent()
    print( r.geeks)
except Exception as err:
    print (err)

r = child()
print (r.geeks)

Output:

Can't instantiate abstract class parent with abstract method geeks
child class

In the above example, the Base class cannot be instantiated because it has only an abstract version of the property getter method.

Thank you. Peace ✌️.


Made with ❤️ by SasiVatsal and Mowli