Hello Folks....!!!
Today let us look at the concept of inheritance with examples.
Inheritance
- It allows a class to derive the properties of another class.
- It can also be considered as defining a new class with little modification to the existing ones without writing the class again from the scratch.
- The main advantage of using inheritance concepts is code re-usability.
- It exhibits transitivity. For example if class B inherits A, class C and D inherits B, then C and D inherits the properties of A also.
- The class which derives the properties is called child/derived class. The class from which it derived is parent/base class. For example B inherits A B-child and A-parent.
- Depending upon the number of classes inherited it can be classified as single or multiple inheritance.
- The parent can be made to hold the attributes and functions that are required in common and the child classes can utilize them.
- Inheritance is 'is-a' relationship which means one is the subclass of the other. For example class B inherits class A, then B is-a subclass of A. Let class A be animals and class B be mammal that inherits animals. So the relationship is mammal is an animal.
Single inheritance
- If the child class is derived from one parent class.
- Concept : one parent - one child.
- For example class B inherits A, class C inherits D
- Now all the instances (objects) created for class B can access the elements in class A, similarly objects of class C can access class D elements.
- The syntax is
class definition
Let us look at an example. Consider creating record of an organization
where thousands of people work.
Let this be the
base class for this
example. The class person has the attributes
name,age which are
common requirement. All the person in the world has name and age. Then the
function for reading the inputs is also provided in the base class.
Now we come to define the next class which is a derived one. The class
employee inherits
the properties of
person. The line
class employee (person) denotes inheritance. Since it inherits only one class 'person' this is of single inheritance concept. This is now the child class.
The child class can have extra attributes and functions as per the
requirement. Here I have added the
designation
and
overtime as
extra attributes and function for reading inputs and one function for
printing details.
Finally, we have to create instances and call the appropriate functions.
The instance is created for the derived class here. So we have the
object e that
corresponds to the class
employee which has
access to the attributes and functions of base class
person.
Using the created object the read function of the base class (read_base) can be called, the child class read function (read_child) can also be called. In the
print_func() function
the attributes of the parent class have also been printed. This is the
concept of single inheritance.
Note that can create instance of the parent class alone and give values
for its attributes also. For example, look at the below.
But if you try to access the the derived class alone and the function
in it is dependent on the base class properties, then you cannot execute
it independently. You will get an error.
Here I have tried skipping the
read_base() function.
But in the
print_func() function we
have coded it to print the
name and age which are
attributes of the base class. Since we did not get any value for it, we
get an error message.
To overcome this problem, there are two solutions;
solution 1: We can write separate functions that allows to access the elements of
derived class alone without any errors. For the same example given above,
look at the code snippet given. You can modify the class slightly.
Here the
print_func1() can be
used while accessing the base class also. When you want to access the
derived one alone you use the
print_func2()
function. Thus you can create an object and call the function like this.
This doesn't produce any error. But this is not an efficient way of
writing code. Let us have a look at the next solution.
Solution 2: We have separate constructors for both the classes. The constructors
initialize the attributes of the class with some default value. To know
about constructors see
this post. So we can call the constructor of the base class in the child class's
constructor. This will initialize the attributes with some default value.
The error wont occur now. This is called
class composition. Look at the code below.
You can even call the
read_base() function
inside
read_child() by
writing
self.personal_det.read_base(). This will get the user input for
name and age also.
Next Topic👉
Comments
Post a Comment