Python Inheritance
Inheritance is the process of creating a new class from the existing class, the newly created class inherits the properties from the existing class. During the process, the existing class remains unchanged.
The new class is called child class or derived class and the main class from which it inherits the properties is called a base class or parent class.
In inheritance, the child class acquires the properties and can access all the data members and functions defined in the parent class. A child class can also provide its specific implementation to the functions of the parent class.
In python, a derived class can inherit base class by just mentioning the base in the bracket after the derived class name. Consider the following syntax to inherit a base class into the derived class.
In python we have the following inheritance:
* Single Inheritance
* Multi-Level inheritance
* Multiple inheritances
Single Inheritance
In single inheritance we have single base class and a single derived class. The single derived class inherits all the properties of the single base class
Syntax
class base_class:
variables
functions
class derived_class(base class):
variables
functions
Example:
class hello: def abc(self): print("function of base class hello") class hi(hello): def xyz(self): print("function of derived class hi") a=hi() a.abc() a.xyz()
We declare an object of the derived class and using it base class member functions are invoked.
Output:
function of base class hello function of derived class hi >>>
Example:
class hello: def abc(self): print("function of base class hello") class hi(hello): def xyz(self): hello.abc(self) print("function of derived class hi") a=hi() a.xyz()
In the above program base class member functions and invoked from within the member functions of derived class.
Output:
function of base class hello function of derived class hi >>>
Example:
Python program to declare a class named student with attributes as roll and name. Declare another class named physical with attributes as age, ht and wt. class physical is the derived class of the class student. Take input for the details and display them.
Sol:
class student: roll=0 name="" def read(self): self.roll=int(input("enter roll ")) self.name=input("Enter name ") def show(self): print("roll no ",self.roll) print("name ",self.name) class physical(student): age=0 ht=0 wt=0 def pread(self): student.read(self) self.age=int(input("Enter age ")) self.ht=int(input("Enter ht ")) self.wt=int(input("Enter wt ")) def pshow(self): student.show(self) print("age ",self.age) print("ht ",self.ht) print("wt ",self.wt) a=physical() a.pread() a.pshow()
Output:
enter roll 1001 Enter name Amit Enter age 15 Enter ht 6 Enter wt 45 roll no 1001 name Amit age 15 ht 6 wt 45 >>>
Example:
Python program to declare a class named student with attributes as roll and name. Declare another class named marks with attributes as m1,m2,m3, total and per. Class marks is the derived class of the class student. Take input for the details and display them.
Sol:
code
Output:
code