DATA ABSTRACTION Notes With Examples
DATA ABSTRACTION Notes With Examples
DATA ABSTRACTION Notes With Examples
Introduction
We use television to watch shows, news or movies, etc. 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. What they
don’t know is how all this is happening internally, for example how the TV sensor is
capturing signals from the TV remote and then how it is processing the received signals
to perform the required action of changing the channel, etc. All the internal functionality
is hidden, as for the user it might not be necessary for them to know how that is
happening.
The example we saw above is one of the examples of abstraction in real life. In object-
oriented programming, we shall call it ‘Data Abstraction’. Let us define data 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.
The main focus of data abstraction is to separate the interface and the implementation
of the program.
Instead, you'd create a program capable of multiplying any two numbers. To put it
another way, abstraction is a way of thinking about a function's specific use as separate
from its more generalized purpose. Thinking this way lets you create flexible, scalable,
and adaptable functions and programs. You’ll get a better understanding of data
abstraction and it’s purposes by the end of this article.
Inheritance in OOP is a way through which one class inherits the attributes and
methods of another class. The class whose properties and methods are inherited is
known as the Parent class. And the class that inherits the properties from the parent
class is the Child class/subclass.
class parent_class:
body of parent class
The abstract class is an interface. Interfaces in OOP enable a class to inherit data and
functions from a base class by extending it. In Python, we use the NotImplementedError
to restrict the instantiation of a class. Any class having this error inside method
definitions cannot be instantiated.
We can understand that an abstract class just serves as a template for other classes by
defining a list of methods that the classes must implement. To use such a class, we
must derive them keeping in mind that we would either be using or overriding the
features specified in that class.
Consider an example where we create an abstract class Fruit. We derive two classes
Mango and Orange from the Fruit class that implement the methods defined in this
class. Here the Fruit class is the parent abstract class and the classes Mango and
Apple become the sub/child classes. We won’t be able to access the methods of the
Fruit class by simply creating an object, we will have to create the objects of the derived
classes: Mango and Apple to access the methods.
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.
isinstance()
and
issubclass()
The abc module provides the metaclass ABCMeta for defining ABCs and a helper class
ABC to alternatively define ABCs through inheritance. The abc module also provides
the @abstractmethod decorator for indicating abstract methods.
ABC is defined in a way that the abstract methods in the base class are created by
decorating with the @abstractmethod keyword and the concrete methods are registered
as implementations of the base class.
The methods where the implementation may vary for any other subclass are defined as
abstract methods and need to be given an implementation in the subclass definition.
On the other hand, there are methods that have the same implementation for all
subclasses as well. There are characteristics that exhibit the properties of the abstract
class and so, must be implemented in the abstract class itself. Otherwise, it will lead to
repetitive code in all the inherited classes. These methods are called concrete methods.
class Parent(ABC):
#common function
def common_fn(self):
print('In the common method of Parent')
@abstractmethod
def abs_fn(self): #is supposed to have different
implementation in child classes
pass
class Child1(Parent):
def abs_fn(self):
print('In the abstract method of Child1')
class Child2(Parent):
def abs_fn(self):
print('In the abstract method of Child2')
An abstract class can have both abstract methods and concrete methods.
We can now access the concrete method of the abstract class by instantiating an object
of the child class. We can also access the abstract methods of the child classes with it.
Interesting points to keep in mind are:
class Animal(ABC):
#concrete method
def sleep(self):
print("I am going to sleep in a while")
@abstractmethod
def sound(self):
print("This function is for defining the sound by any
animal")
pass
class Snake(Animal):
def sound(self):
print("I can hiss")
class Dog(Animal):
def sound(self):
print("I can bark")
class Lion(Animal):
def sound(self):
print("I can roar")
class Cat(Animal):
def sound(self):
print("I can meow")
Our abstract base class has a concrete method sleep() that will be the same for all the
child classes. So, we do not define it as an abstract method, thus saving us from code
repetition. On the other hand, the sounds that animals make are all different. For that
purpose, we defined the sound() method as an abstract method. We then implement it
in all child classes.
Now, when we instantiate the child class object, we can access both the concrete and
the abstract methods.
c = Cat()
c.sleep()
c.sound()
c = Snake()
c.sound()
Now, if we want to access the sound() function of the base class itself, we can use the
object of the child class, but we will have to invoke it through super().
class Rabbit(Animal):
def sound(self):
super().sound()
print("I can squeak")
c = Rabbit()
c.sound()
class Deer(Animal):
def sound(self):
pass
c = Deer()
c.sound()
c.sleep()
This will produce the following error:
NOTE: Had there been more than one abstract method in the base class, all of them
are required to be implemented in the child classes, otherwise, an error is produced.
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.
Conclusion
Now that we have learned about Data Abstraction in Python, recall and tryto answer
some questions for your better understanding: