What is Inheritance (Is-A Relation)?

It is a concept of Object-Oriented Programming. Inheritance is a mechanism that allows us to inherit all the properties from another class. The class from which the properties and functionalities are utilized is called the parent class (also called as Base Class). The class which uses the properties from another class is called as Child Class (also known as Derived class). Inheritance is also called an Is-A Relation

Inheritance – diagrammatic representation

In the figure above, classes are represented as boxes. The inheritance relationship is represented by an arrow pointing from Derived Class(Child Class) to Base Class(Parent Class). The extends keyword denotes that the Child Class is inherited or derived from Parent Class

Syntax :   

# Parent class
class Parent :        
           # Constructor
           # Variables of Parent class

           # Methods
           ...

           ...


# Child class inheriting Parent class 
class Child(Parent) :  
           # constructor of child class
           # variables of child class
           # methods of child class

           ...

           ... 

Example : 

Python3




# parent class
class Parent:
  
    # parent class method
    def m1(self):
        print('Parent Class Method called...')
  
# child class inheriting parent class
class Child(Parent):
  
    # child class constructor
    def __init__(self):
        print('Child Class object created...')
  
    # child class method
    def m2(self):
        print('Child Class Method called...')
  
  
# creating object of child class
obj = Child()
  
# calling parent class m1() method
obj.m1()
  
# calling child class m2() method
obj.m2()


Output

Child Class object created...
Parent Class Method called...
Child Class Method called...

Inheritance and Composition in Python

This article will compare and highlight the features of is-a relation and has-a relation in Python.

Similar Reads

What is Inheritance (Is-A Relation)?

It is a concept of Object-Oriented Programming. Inheritance is a mechanism that allows us to inherit all the properties from another class. The class from which the properties and functionalities are utilized is called the parent class (also called as Base Class). The class which uses the properties from another class is called as Child Class (also known as Derived class). Inheritance is also called an Is-A Relation....

What is Composition (Has-A Relation)?

...

Composition vs Inheritance

It is one of the fundamental concepts of Object-Oriented Programming. In this concept, we will describe a class that references to one or more objects of other classes as an Instance variable. Here, by using the class name or by creating the object we can access the members of one class inside another class. It enables creating complex types by combining objects of different classes. It means that a class Composite can contain an object of another class Component. This type of relationship is known as Has-A Relation....

Contact Us