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.