Introduction
The concept of inheritance is used in the context of
object oriented modeling. Its key objective is to reduce the redundancy in
object models by arranging classes with similar attributes and operations in a
hierarchy.
Inheritance
In object-oriented programming, inheritance enables
new objects to take on the properties of existing objects. A class that is used
as the basis for inheritance is called a superclass or base class. A class that
inherits from a superclass is called a subclass or derived class. The terms
parent class and child class are also acceptable terms to use respectively. A
child inherits visible properties and methods from its parent while adding
additional properties and methods of its own.
Subclasses and superclasses can be understood in
terms of the is a relationship. A subclass is a more specific instance of a
superclass.
Inheritance hierarchyInheritance is a type of
relationship among classes, wherein one class shares the structure or behaviour
defined in one (single inheritance) or more (multiple inheritance) other
classes. Inheritance defines a "kind of" hierarchy among classes in
which a sub class inherits from one or more super-classes; a sub-class
typically augments or redefines the existing structure and behaviour of its
super-classes.
The example models a fragment of a complaint
resolution system. It models the fact that a complaint can either be about the
company's product or a product purchased by the company from a supplier. The
former complaint would originate from a customer, the latter would originate
from within the company itself. In both cases the complainant details, date and
description of complaint are recorded. These attributes therefore remain with
the super-class "complaint" which models the general case. In the
special case of the company product however invoice, credit note and product
information needs to be recorded. These attributes are therefore modeled as
being unique to the "company product" sub-class. In the case of the supplier
product, supplier information, order number, supplier invoice and supplier
product information must be recorded. These attributes are therefore unique to
the "supplier product" sub-class. In both cases the attributes of the
super-class "complaint" plus the attributes of the individual
sub-classes completely define the attributes of that sub-class.
In both cases the complaint needs to be recorded and
resolved. These operations therefore remain with the super-class
"complaint". In the case of a sub-class "company product" a
credit note needs to be recorded. This operation therefore augments the
operations of this sub-class. As with the attributes the operations of the
super-class "complaint" plus the operations of the individual
sub-classes completely define the operations of that sub-class.
The sub-classes are said to inherit the attributes
and operations of their super-class.
Rules for Defining Sub-classes
A sub-class is a specialisation of its parent class
All sub-classes inherit the super-class attributes
& operations
Each sub-class has unique attributes &
operations
A sub-class may override features of a parent class
by defining one with the same name
Dealing
With Multiple Inheritance
Multiple inheritance permits a class to have more
than one super-class and to inherit features from all parents.
A class may inherit features from more than one
super-class
If parent classes have the same features they are
inherited only once
Conflicts among parallel features must be explicitly
resolved by disabling the inheritance of a specific feature
In the example an amphibious vehicle has the
characteristics of both a land vehicle and a water vehicle. It therefore
exhibits multiple inheritance from both these classes.
No comments:
Post a Comment