x

Python Inheritance

PREV    NEXT

Example 6:

Inheritance is divided 5 types, They are


  1. Single or Simple Inheritance
  2. Multiple Inheritances
  3. Multi Level Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

1. Single or Simple Inheritance:

In this type of inheritance only one level of inheritance available, it means one base/parent class and one derive/child class

Below is the example for single or simple inheritance

Output:

2. Multiple Inheritance:

In this type of Inheritance one derive class created using more than on base class

Example

Output:

3. Multilevel Inheritance:

In this type of Inheritance more than one derived class created by using one base class

Example:

Output:

4. Hierachical Inheritance:

In this type of inheritance from one base class more than one derived class is derived.


5. Hybrid Inheritance:

Hybrid Inheritance is a combination of Multiple and Hierarchical Inheritance.

Overriding Methods:

Having same function name and function arguments in base class and as well as in derive class ,then we can call it as Overridden Methods.It is only possible in Inheritance.

Example:

Output:

Example 7:

Encapsulation:

  • Wrapping of data and method,method should work on that data is called Encapsulation,i.e capsules.
  • By using Encapsulation we can achieve the data protections,it means nobody can access the data from the outside of the class,It will restrict to access the data and methods.

Example:

Output:

Note:
If we want change the data then we need to new method to set that data

Ex:

Output:

PREV    NEXT



Like it? Please Spread the word!