Why Is the Concept of Inheritance Important in Object-Oriented Programming?
Object-oriented programming languages use inheritance so that software designers can create logical class hierarchies and can better organize, encapsulate and reuse code. As a result, programs are easier to design, modify and maintain and code libraries have easier-to-use interfaces.
-
Sharing What is Common
-
A base or root object in a class hierarchy can define properties and methods common to all classes derived from it.
Specializing
-
A derived class can use all properties and methods that the base class provides and can define new properties and methods specific to its needs.
-
Redefining
-
If need be, a derived class can change the definition of base class methods to make these useful in a way specific to itself.
Dynamic Binding
-
Because a derived class is a more specific version of its base class, client code (code that makes use of a class library) can be designed to use the base class interface and yet operate on an instance of a derived class without problem.
Reuse and Encapsulation
-
A programmer need not rewrite the same code over and over again for each class type. Instead, derived classes automatically reuse code defined in their parent classes. This code can be redefined as needed to supply specific functionality. In addition, client code can be written to use methods defined in a base class but actually operate on instances of the derived class. The client code need not be aware that it is actually operating on a derived class instance. In this way one client method can operate on different derived object instances in an identical way using minimal code.
-
References
- "C++ Primer, Fourth Edition;" Stanley B. Lippman, Josée Lajoie, Barbara E. Moo; 2005