1/14
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
Inheritance
One of the core concept in object oriented programming.
It is a mechanism that allows you to create hierarchy of classes that share a set of properties and method by deriving a class from another class.
Inheritance is to capability of one class to derive or inherit the properties from another class.
Allows you to inherit the properties of a class example based glass to another derived class.
Super Class (Parent/Base Class)
The class whose feature are inherited by another classes.
Sub Class (Child Class)
The class that inherits the feature of other classes. subclasses can also have their own features in addition to inherit one.
Reusability
The concept where we can derive a new class from existing classes.
Benefits of Inheritance in Python
It represents real world relationships well.
It provides the reusability of a code. We don't have to write the same code again and again. Also, it allows us to add more features to a class without modifying it.
It is transitive in nature which means that if class why it herits from another class x then all the subclasses of y would automatically inherit from class x.
Inheritance offer a simple, understandable modal structure
Less development and maintenance expenses result from an inheritance.
IS-A relationship
To further understand this mechanism of OOP, we use the IS-A relationship, where we create a subclass by including the name of the parent class as a parameter.
Considering the IS-A relationship, we can say that:
Sedan IS-A Car
Pickup IS-A Car
Toyota IS-A Sedan and Pickup
Hence: Toyota IS-A Car
Subclassing
A child class need to identify which class is its parent class.
This can be done by mentioning the parent class name in the definition of the child class.
In order to properly use the attributes of a parent class you must either:
Invoke the constructor of the parent class
Do not create a constructor for the subclass and use getter and setters instead
Note: You can also use the super() function to access the parent’s class methods and functions.
Single Inheritance
Types of Inheritance:
Single inheritance enables a derived class to inherit properties from a single parent class, thus enabling code reusability and the addition of new features to existing code.
Multiple Inheritance
Types of Inheritance:
When a class is derived from more than one base class, this type of inheritance is called multiple inheritance.
In multiple inheritance, all the features of the base classes are inherited into the derived class.
Multilevel Inheritance
Types of Inheritance:
In multilevel inheritance, features of the base class and the derived class are further inherited into the new derived class.
This is similar to a relationship representing a child and a grandfather.
Hierarchical Inheritance
Types of Inheritance:
When more than one derived class are created from a single base, this type of inheritance is called hierarchical inheritance.
In this program, we have a parent (base) class and two or more child (derived) classes.
Hybrid Inheritance
Types of Inheritance:
Inheritance considering of multiplle types of inheritance is called hybrid inheritance.
Polymorphism
The word polymorphism means having many forms.
In programming, polymorphism means the same function name (but different signatures) being used for different types.
The key difference is the data types and number of arguments used in function.
Polymorphism with class methods
Python can use two different classes in the same way.
We create a for loop that iterates through a tuple of objects then call the methods without being concerned about which class type each object is.
We assume that these methods actually exist in each class.
Polymorphism with Inheritance
In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class.
However, it is possible to modify a method in a child class that it has inherited from the parent class.
This is particularly useful in cases where the method inherited from the parent class doesn’t quite fit the child class.
In such cases, we re-implement the method in the child class. This process of re-implementing a method in the child class is known as Method Overriding.