meanings of Inheritance-oop encyclopedia of Inheritance-oop dictionary of Inheritance-oop thesaurus on Inheritance-oop books about Inheritance-oop dreams about Inheritance-oop
 Inheritance-oop - Definition 

See inheritance (computer science) for other computing uses of inheritance.

In object-oriented programming of computer science, an inheritance is a way to form new classes (instances of which will be 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.

Contents

Applications of inheritance

There 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.

Specialization

One 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 that 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.

Overriding

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 is not to be overridden.

Extension

Another 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-use

One 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 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.

Limitations and Alternatives

When using inheritance extensively in designing a program, one should be aware of certain constraints that it imposes.

For example, consider a class Person that contains a person's name, address, phone number, age, sex, and race. We can define a subclass of Person called Student that contains the person's grade point average and classes taken, and another subclass of Person called Employee that contains the person's job title, employer, and salary.

In defining this inheritance hierarchy we have already defined certain restrictions, not all of which are desirable:

Constraints of inheritance-based design

  • Singleness: using single inheritance, a subclass can inherit from only one superclass. Continuing the example given above, Person can be either a Student or an Employee, but not both. Using multiple inheritance partially solves this problem, as a StudentEmployee class can be defined that inherits from both Student and Employee. However, it can still inherit from each superclass only once; this scheme does not support cases in which a student has two jobs or attends two institutions.
  • Staticness: the inheritance hierarchy of an object is fixed at instantiation when the object's type is selected and does not change with time. For example, the inheritance graph does not allow a Student object to become a Employee object while retaining the state of its Person superclass.
  • Visibility: whenever client code has access to an object, it generally has access to all the object's superclass data. Even if the superclass is not a public one, the client can still cast the object to its superclass type. For example, there is no way to give a function a pointer to a Student's grade point average and transcript without also giving that function access to all of the personal data stored in the student's Person superclass.

Roles and inheritance

Sometimes inheritance based design is used instead of roles. A role, say Student role of a Person describes a characteristic associated to the object that is present because the object happens to participate in some relationship with another object (say the person in student role -has enrolled- to the classes). Some object-oriented design methods do not distinguish this use of roles from more stable aspects of objects. Thus there is a tendency to use inheritance to model roles, say you would have a Student role of a Person modelled as a subclass of a Person. However, neither the inheritance hierarchy nor the types of the objects can change with time. Therefore, modelling roles as subclasses can cause the roles to be fixed on creation, say a Person cannot then easily change his role from Student to Employee when the circumstances change. From modelling point of view, such restrictions are often not desirable, because this causes artificial restrictions on future extensibility of the object system, which will make future changes harder to implement, because existing design needs to be updated. Inheritance is often better used with a generalization mindset, such that common aspects of instantiable classes are factored to superclasses; say having a common superclass 'LegalEntity' for both Person and Company classes for all the common aspects of both. The distinction between role based design and inheritance based design can be made based on the stability of the aspect. Role based design should be used when it's conceivable that the same object participates in different roles at different times, and inheritance based design should be used when the common aspects of multiple classes (not objects!) are factored as superclasses, and do not change with time.

One consequence of separation of roles and superclasses is that compile-time and run-time aspects of the object system are cleanly separated. Inheritance is then clearly a compile-time construct. Inheritance does influence the structure of many objects at run-time, but the different kinds of structure that can be used are already fixed at compile-time.

Component-oriented design as an alternative to inheritance

An alternative way to design the aforementioned system of persons, students, and employees would be to define helper classes Transcript and Job to store the additional information for a student and employee, respectively. Then, each Person object can contain a collection of Transcript objects and a collection of Job objects. This removes the aforementioned constraints:

  • A Person can now have an arbitrary number of jobs and attend an arbitrary number of institutions
  • These jobs can now be changed, added, and deleted dynamically
  • It is now possible to pass a student's Transcript to a function--for example, one that makes college admissions decisions--without also automatically passing in the student's name, age, race, and other personal information.

Using composition instead of inheritance also leads to less ambiguous syntax. For example, in a inheritance-oriented design, one might encounter the following code: Employee e = getEmployee(); print(e.jobTitle()); While one might infer that the jobTitle() function is defined in the Employee class, it is also possible that it is defined in the Person class. As the inheritance hierarchy gets deeper, this effect, known as the yo-yo problem, is magnified.

A composition-based design allows the programmer to maintain a shallower inheritance hierarchy, and minimizes ambiguity, as in the following example: Person p = getPerson(); print(p.job().title()); To a programmer that knows the Job class has no superclass, it is immediately obvious that the title() function is defined in the Job</class>.

Component-oriented design cannot be substituted for an inheritance-based design in all cases. For example, inheritance enables polymorphism and encapsulation. Also, defining component classes instead of subclasses can increase the length of source code.


See also


de:Vererbung (objektorientierte Programmierung)pl:Dziedziczenie w programowaniu obiektowym

Copyright 2008 WordIQ.com - Privacy Policy  ::  Terms of Use  :: Contact Us  :: About Us
This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article "Inheritance-oop".