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

COMPILE & RUN

Output:

2. Multiple Inheritance:

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

Example

COMPILE & RUN

Output:

3. Multilevel Inheritance:

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

Example:

COMPILE & RUN

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:

COMPILE & RUN

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:

COMPILE & RUN

Output:

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

Ex:

COMPILE & RUN

Output:

PREV    NEXT


Share this website !!!
Facebook Twitter Linkedin