| word looked up : | home / archive |
Inheritance (object-oriented programming)In computer science's object-oriented programming theory, inheritance is a programming language feature which allows one class or object to incorporate data or behavioral facets of another. Typically the incorporated facets can be presented as being provided directly by the incorporating class or object. In some cases the incorporated facets can be altered.Complex inheritances may cause Yo-yo problem. The concept of inheritance is also used in other areas of computer science, such as Cascading Style Sheets.
| |||
Another form of specialization occurs when an inherited class specifies that it has a particular behavior but doesn't 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.
Many 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 isn't to be overridden.
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.
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 doesn't 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 isn't 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.
A Java program might have a class Animal that contained such data elements as whether the animal was presently alive, where it is currently located, etc.; as well as methods instructing the animal to eat, move, mate, etc. If we wanted to create a class Mammal, most of those data elements and functions would be the same as for most animals, but a few would change. We therefore define Mammal as a subclass of Animal (we then say that Animal is Mammal's superclass or parent 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 reproduce method to add functionality. Within the reproduce method, note the call super.reproduce(). This is a call to the superclass method which we are redefining. This roughly means "do whatever a member of my superclass would do", which is then followed by code specific to our new subclass.
See Multiple inheritance.
See also:
They both listened attentively, but could hear
looked abroad; it was a fine, calm night.html">night, and myriads of twinkling
the yard and hay-yard; but all was calm and lonely, and no sound.html">sound.html">sound
cur, or the sluggish murmuring of the little tortuous river in
replenished the expiring fire, and sat down.html">down to finish whatever
without.
"The banshee again," said Moya, faintly. Jack O'Malley's soul
caught his arm. "No, no, Harry, you shall not; sit down; there's
his tobacco-pipe, whilst the old woman was on her knees, striking
now seemed to proceed from the window, and again it appeared as
afar, whilst again it would appear as if coming down the chimney
resembled the low, plaintive wail of a female in distress, and in
as if coming from a thousand throats; now the sound resembled a low,
demoniac laugh. It continued thus, with little intermission, for
by a heavy, creaking sound, as if of some large waggon, amidst
accompanied with a strong, rushing wind. This strange noise
down the lane which led to the road, and was heard no more. Jack
and scepticism, was astonished and frightened.
"A dreadful night this, Moya," said Jack.
"Yes," said she, "that is the dead-coach; I often heard it before,
as big, and hung over with black.html">black cloth, and a black coffin on the
person, and I wonder what brought it now, unless it came with the
smile.
"No, no," she said; "I am none of that family at all at all."
A solemn silence now ensued for a few minutes, and they thought.
On
wordlookup.net
All is still licensed under the GNU FDL.
It uses material from the wikipedia.
|
|