Inheritance
Inheritance
Inheritance
In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects, establish a subtype from an existing object, or both, depending upon programming language support. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior (i.e., previously coded algorithms associated with a class) from pre-existing classes called base classes or superclasses or parent classes or ancestor classes. The new classes are known as derived classes or subclasses or child classes. The relationships of classes through inheritance gives rise to a hierarchy. In prototype-based programming, objects can be defined directly from other objects without the need to define any classes, in which case this feature is called differential inheritance. Complex inheritance, or inheritance used within an insufficiently mature design, may lead to the yo-yo problem.
Subclasses and superclasses
A subclass, or child class, is a modular, derivative class that inherits one or more properties from another class (called the superclass, base class, or parent class). The superclass establishes a common interface and foundational functionality, which specialized subclasses can inherit, modify, and supplement. Because it offloads specialized operations to a subclass, a superclass is more reusable. A subclass may customize or redefine a method inherited from the superclass. A method redefined in this way is called a virtual method
Applications
Inheritance is used to co-relate two or more classes to each other. With the use of inheritance we can use the methods and the instance variables of other classes in any other classes.
Overriding
Many object-oriented programming languages permit a class or object to replace the implementation of an aspecttypically a behaviorthat it has inherited. This process is usually called overriding. Overriding introduces a complication: which version of the behavior does an instance of the inherited class usethe one that is part of its own class, or the one from the parent (base) class? The answer varies between programming languages, and some languages provide the ability to indicate that a particular behavior is not to be overridden and behave according to the base class. For instance, in C#, the overriding of a method should be specified by the program. An alternative to overriding is hiding the inherited code.
Single Inheritance