Inheritance in Object Oriented Programming (OOP)
A concept that allows one class to inherit the properties and behaviours (i.e., fields and methods) of another class.
Inheritance promotes code reusability and helps in structuring and organizing code.
In inheritance we have:
1. Base Class (Parent Class or Superclass):
The class whose properties and methods are inherited by another class.
2. Derived Class (Subclass or Child Class):
The class that inherits from a base class is known as the derived class or subclass.
Syntax:
Imagine we have a "Vehicle" class. This class defines what all vehicles have in common, like wheels and an engine.
Now, if we want to create a "Car" class, you can make it inherit from the "Vehicle" class.
So, inheritance helps us avoid repeating the same code and makes it easier to create new classes that share common features with existing classes.
A concept that allows one class to inherit the properties and behaviours (i.e., fields and methods) of another class.
Inheritance promotes code reusability and helps in structuring and organizing code.
In inheritance we have:
1. Base Class (Parent Class or Superclass):
The class whose properties and methods are inherited by another class.
2. Derived Class (Subclass or Child Class):
The class that inherits from a base class is known as the derived class or subclass.
Syntax:
class BaseClass {
};
class DerivedClass : access-specifier
BaseClass {
};Imagine we have a "Vehicle" class. This class defines what all vehicles have in common, like wheels and an engine.
Now, if we want to create a "Car" class, you can make it inherit from the "Vehicle" class.
So, inheritance helps us avoid repeating the same code and makes it easier to create new classes that share common features with existing classes.
❤1
Types of Inheritance
1. Single Inheritance:
In single inheritance, a class can inherit from only one parent class. This is the simplest form of inheritance. The derived class inherits the attributes and methods of a single base class.
2. Multiple Inheritance:
Multiple inheritance allows a class to inherit from more than one parent class. In this case, the derived class inherits the attributes and methods of multiple base classes.
3. Multilevel Inheritance:
In multilevel inheritance, a class derives from another class, which in turn derives from another class, creating a chain or hierarchy of classes.
4. Hierarchical Inheritance:
In hierarchical inheritance, multiple derived classes inherit from a single base class. This creates a tree-like structure of classes, with a common base class.
5. Hybrid Inheritance:
Hybrid inheritance is a combination of two or more types of inheritance within the same program.
1. Single Inheritance:
In single inheritance, a class can inherit from only one parent class. This is the simplest form of inheritance. The derived class inherits the attributes and methods of a single base class.
2. Multiple Inheritance:
Multiple inheritance allows a class to inherit from more than one parent class. In this case, the derived class inherits the attributes and methods of multiple base classes.
3. Multilevel Inheritance:
In multilevel inheritance, a class derives from another class, which in turn derives from another class, creating a chain or hierarchy of classes.
4. Hierarchical Inheritance:
In hierarchical inheritance, multiple derived classes inherit from a single base class. This creates a tree-like structure of classes, with a common base class.
5. Hybrid Inheritance:
Hybrid inheritance is a combination of two or more types of inheritance within the same program.