Inheritance object-oriented programming
In object-oriented programming of computer science, an inheritance is a way to form new classes or objects using pre-defined objects or classes where new ones simply take over old ones's implemetions and characterstics. It is intended to help reuse of existing code with little or no modification. Complex inheritances may cause the Yo-yo problem.
Applications of inheritanceThere are many different aspects to inheritance. Different uses focus on different properties, such as the external behaviour of objects, internal structure of the object, structure of the inheritance hierarchy, or software engineering properties of inheritance. Sometimes it's desirable to distinguish these uses, as it's not necessarily obvious from context. SpecializationOne common reason to use inheritance is to create specializations of existing classes or objects. This is often called subtyping when applied to classes. In specialization, the new class or object has data or behavior aspects which are not part of the inherited class. For example, a "Bank Account" class might have data for an "account number", "owner", and "balance". An "Interest Bearing Account" class might inherit "Bank Account" and then add data for "interest rate" and "interest accrued" along with behavior for calculating interest earned. Another form of specialization occurs when an inherited class specifies that it has a particular behavior but does not actually implement the behavior. Each class which inherits from that abstract class must provide an implementation of that behavior. This providing of actual behavior by a subclass is sometimes known as implementation or reification. OverridingMany object-oriented programming languages permit a class or object to replace the implementation of an aspect—typically a behavior—that it has inherited. This process is usually called overriding. Overriding introduces a complication: which version of the behavior does code from the inherited class see—the one that is part of its own class, or the overriding behavior? The answer varies between programming languages, and some languages provide the ability to indicate that a particular behavior is not to be overridden. ExtensionAnother reason to use inheritance is to provide additional data or behavior features. This practice is sometimes called extension or subclassing. In contrast to the case of specialization, with extension the new data or behaviors could have been provided in the inherited class because they are generally applicable to all instances of the class. Extension is often used when incorporating the new features into the inherited class is either not possible or not appropriate. It can also be used at the object level, such as in the Decorator pattern. Code re-useOne of the earliest motivations for using inheritance was to allow a new class to re-use code which already existed in another class. This practice is usually called implementation inheritance. In most quarters, class inheritance for the sole purpose of code re-use has fallen out of favor. The primary concern is that implementation inheritance does not provide any assurance of polymorphic substitutability—an instance of the re-using class cannot necessarily be substituted for an instance of the inherited class. An alternative technique, delegation, requires more programming effort but avoids the substitutability issue. The notion that implementation inheritance should be avoided is not universal. One prominent object-oriented programming expert who believes that implementation inheritance has its place is Bertrand Meyer. In his book Object Oriented Software Construction, 2nd ed., Meyer lists twelve different uses of inheritance that he considers to be legitimate, most of which involve some amount of implementation inheritance. An example of inheritanceA Java program might have a class
class Mammal extends Animal {
Hair m_h;
Breasts m_b;
Mammal reproduce() {
Mammal offspring;
super.reproduce();
if (self.is_female()) {
offspring = super.give_birth();
offspring.breastfeed(m_b);
}
care_for_young(offspring);
return offspring;
}
}
Note here that we don't need to specify that a mammal has all the usual animal things: a location, ability to eat, move, etc. We do add some additional features such as hair and breasts that are unique to mammals, and we redefine the See also
Categories: Object-oriented programming |
|
This article is licensed under the GNU Free Documentation License. It uses material from Wikipedia article. Browse Wikipedia for more information. |