14. OOPS Concepts - - Inheritance and Polymorphism
Modularity: The act of dividing a program into individual components is called modularity. The justification for partitioning a program is that:
- It reduces its complexity to some degree.
- It creates a number of well-defined, documented boundaries within the program.
The modularity in languages like C++ and Java is implemented by writing code separately in files (called classes) and organizing them in separate directories (called namespaces or packages).
Inheritance: Inheritance is the process by which an object of one class acquires the properties of objects of another class.
There are several reasons why inheritance was introduced into OO(Object Oriented) languages.
- The main reason is reusability i.e., one can derive a new class from an existing class and add new features to it.
- It ensures the closeness with real world models.
- Another reason is transitive nature. If a class A inherits properties of another class B, then all subclasses of A will automatically inherit the properties of B.
Polymorphism:
Polymorphism can be defined as the ability of a single entity to behave differently depending on the context. It has the ability to take more than one form. i.e., an operation may exhibit different behaviours in different instances.