Definition Definition

Inheritance

Inheritance is the most important feature of Object-Oriented Programming.

Inheritance in Object-oriented programming is a mechanism to make the code reusable by defining a child class (subclass) of a parent class (base class) in which subclass inherits properties and methods of parent class.

Subclass: A class that is derived from another class is called a subclass (derived class or extended class, or child class).

Base Class: The class from which the subclass is derived is called a base class (super class or parent class).

Advantage of inheritance:

  • Method Overriding (so runtime polymorphism can be achieved).
  • Code Reusability. (make the program small and clean) 

Example:

A Car has all the features of a vehicle. If we define a base class ‘Vehicle’ and a ‘Car’ class which derived from ‘Vehicle’. Then the ‘Car’ class inherits the features of ‘Vehicle’ class and also has its own attributes. We can also extend the ‘Car’ class to ‘Sports Car’ which inherits the features of both ‘Vehicle’ and ‘Car’ classes.

Syntax (Java):

class Subclass-name extends Superclass-name  
{  
   //methods and fields  
}  

Share it: CITE

Related Definitions