Inheritance
Inheritance is another way of dealing with laziness (in the positive sense).
Programmers want to avoid typing the same code more than once. We avoided that earlier by
making functions, to make one that is very similar? Perhaps one that adds only a few methods?
When making this new class, you don’t want to have to copy all the code from the old one over
to the new one.
For example : You may already have a class called Shape, which knows how many side of itself.Now you want to make a class called Rectangle, which also knows how many
side of itself, but which can, in addition, calculate its no. of diagonal.You wouldn’t want to
do all the work of making a new method when Shape has one that works just fine. So what do
you do? You let Rectangle, inherit the methods from Shape. You can do this in such a way
that when Side is called on a Rectangle object, the method from the Shape class is called
automatically.
Output
- Multilevel Inheritance
Multilevel inheritance is also possible in Python like other Object
Oriented programming languages.
We can inherit a derived class from another derived class, this process is
known as multilevel
inheritance. In Python, multilevel inheritance can be done at any depth.
Example:
Output:
- Multiple Inheritance
Like C++, a class can be derived from more than one base classes in
Python. This is called multiple
inheritance. In multiple inheritance, the features of all the base classes
are inherited into the
derived class. The syntax for multiple inheritance is similar to single
inheritance.
Example:
why super() keyword?
The super() methode is most commonly used with __init__ function in base class. This is usually the
only place where we need to do some things in a child then complete the initilization in the parent.
Private members of parent class
We don’t always want the instance variables of the parent class to be inherited by the child class i.e. we can make some of the instance variables of the parent class private, which won’t be available to the child class. We can make an instance variable by adding double underscores before its name.
For example,
Output:
Encapsulation
Wrapping a piece of code in a function is called encapsulation.One of the
benefits of encapsultion
is that it ataches a name to the code, which serves as a kind of
documentation.Another advantage is
that if you reuse the code, it is more concise to call a function twice
than to copy and paste the
body.
Polymorphism
The term polymorphism is derived from a Greek word meaning “having multiple forms.”. We will create a structure that can take or use many forms of objects.
Example of inbuilt polymorphic functions :
You’ve already encountered them in the form of string, list, and dictionary methods. There, too, you
saw some polymorphism:
>>> 'abc'.count('a') 1
>>> [1, 2, 'a'].count('a') 1
In first example "abc' is string AND in second [1,2,'a'] is a list but both have same function count('a')
this count() function have many form it use with list,dict,sets and also with string.
Method Overloading
Python does not supports method overloading. We may overload the methods but can only use the latest
defined method.
Example:
This is the output of second product methode |
Overriding Methods
You can always override your parent class methods. One reason for overriding parent's methods is that
you may want special or different functionality in your subclass.
Example :
Output:
Abstraction
This property allows us to hide the details and expose only the essential features of a concept or object. For example, a person driving a scooter knows that on pressing a horn, sound is emitted, but he has no idea about how the sound is actually generated on pressing the horn.
Thank you
0 Comments