008
INHERITANCE
CASE-STUDY OF OOP
PRINCE
http://www.tch2get.blogspot.com
1/17/2008
INDEX
INTRODUCTION
• What is inheritance?
• Concept of inheritance
FORMS OF INHERITANCE
• Specialization
• Specification
• Construction
• Generalization
• Extension
• Limitation
• Combination
NEED OF INHERITANCE
APPLICATION OF INHERITANCE
• Specialization
• Overriding
• Code re-use
2 | Page
CONSTRAINTS OF INHERITANCE-
BASED DESIGN
• Singleness
• Static
• Visibility
3 | Page
RULES OF INHERITANCE
BENEFIT OF INHERITANCE
• Software Reusability
• Code sharing
• Consistency of inheritance
• Software component
• Rapid prototyping
• Information hiding
COST OF INHERITANCE
• Execution speed
• Program size
• Program complexity
4 | Page
5 | Page
INHERITANCE
INTRODUCTION
The major change occurred for the VB
programmers was when Microsoft announced the launch
of their new VB.NET to become the best successor for
our most favorite programming language Visual Basic.
6 | Page
What Is Inheritance?
Different kinds of objects often have a
certain amount in common with each other. Mountain
bikes, road bikes and tandem bikes, for example, all
share the characteristics of bicycles (current speed,
current pedal cadence, current gear). Yet each also
defines additional features that make them different:
tandem bicycles have two seats and two sets of handle
bars; road bikes have drop handlebars; some mountain
bikes have an additional chain ring, giving them a lower
gear ratio.
7 | Page
a belongs relation can be established) and cognitive
economy (less information needs to be stored about
each specific entity, only its particularities).
In programming
languages INHERITENCE means that the behavior
and data associates with child classes are always
an extension (that is a large set) of the
properties associated with parent classes.
Subclasses must have all the properties of the
8 | Page
parent class, and other properties as well. On the
other hand, since a child class is more
specialized form of parent class, it is also in
certain sense, a contradiction of the parent type.
This tension between inheritance as expansion
inheritance as contradiction is a source for much
of the power inherent in the technique, but in the
same time it causes much confusion as to its
proper employment .
Inheritance is always
transitive, so that a class can inherit features
from super classes many levels away. That is, if
class Dog is a subclass of class Mammal, and
class Mammal is a subclass Animal, then dog will
inherit attributes from both Mammals as well as
of Animals.
Definition: Inheritance
subclass).
class.
class A
public:
integer d;
};
class B : public A
public:
};
10 | P a g e
acquires all of its methods and data. We can then
instantiate an object of class B and call
void func()
B b;
b.d = 10;
};
CONCEPT OF INHERITANCE
DEFINITION (INHERITANCE) : Inheritance is
the mechanism, which allows a class A to inherit
the properties of class B. We say “A inherits from
B”. Objects of class A thus have access to
attributes and methods of class B without the
need to redefine them. The following definition
defines two terms which we are able refer to
participating classes when they use inheritance.
Bicycle
Tandem
Mountain Road bike
bike
bike
12 | P a g e
This gives Mountain Bike all the sane
fields and methods as Bicycles, yet allows its
code to focus exclusively on the features that
make it unique. This makes code for your
subclasses easy to read. However, you must take
care to properly document the state and
behavior that each super class defines, since
that code will not appear in the source file of
each subclass.
13 | P a g e
SUBTYPES AND STRONG TYPING
FORMS OF INHERITANCE
Inheritance is used in a
variety of ways. In this section we will describe a
few of its more common uses. Note that the
following list represents general abstract
categories and is not intended to be exhaustive.
Furthermore, it sometimes happens that two or
more description are applicable to a single
situation , because some methods in a single
class use inheritance in one way while others use
it in another.
Specialization:
The child class is a special case of the
parent class; in other words the child class is a
subtype of the parent class.
14 | P a g e
Probably the most common use of inheritance
and sub classing is for specialization. In sub
classing for specialization, the new class is a
specialized form of the parent class but specifies
the specification the parent in all relevant
respect. Thus, in this form of principle of
substitutability is explicitly upheld. Along with
the following category (sub classing for
specification) this is the most ideal form of
inheritance, and something that a good design
should strive for.
Specification:
The parent class defines behavior that is
implemented in the child class but not in the
parent class.
15 | P a g e
incomplete abstract specification. In such cases
the parent classes sometime known as an
abstract specification class.
16 | P a g e
Construction:
This child class makes use of the behavior
provided by the parent class, but is not a subtype
of the parent class.
Generalization:
The child class modified or overrides some of the
method of the class.
17 | P a g e
the parent class to create more general kind of object.
Sub classing for generalization is often applicable when
we build on a base of existing classes that we do not
wish to modify, or cannot modify.
Extension:
The child class adds new functionally to the parent
class, but does not change any inherited behavior.
19 | P a g e
Sub classing for limitation is an explicit
contravention of the principle of substitutability, and
because it builds subclasses that are not subtypes, it
should be avoided whenever possible.
Variance:
The child class and parent class are variants
of each other, and the class-subclass relationship is
arbitrary.
Combination:
The child class inherits features from more
than one parent class. This is multiple inheritance.
20 | P a g e
characteristics of both a teacher and a student, and can
therefore logically behave as both. The ability of class to
inherit from two or more parent classes is known as
multiple inheritances. It is sufficiently subtle and
complex.
Forms inheritance.
Applications of inheritance
There are many different aspects to inheritance.
Different uses focus on different properties, such as the
external behavior of objects, internal structure of the
object, structure of the inheritance hierarchy, or
software engineering properties of inheritance.
21 | P a g e
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 sub typing 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.
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 an instance of the inherited class use—the 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.
22 | P a g e
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.
23 | P a g e
Limitations and alternatives
When using inheritance extensively in designing a
program, one should be aware of certain constraints
that it imposes.
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 Student Employee
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.
24 | P a g e
superclass. (Although similar behavior can be achieved
with the decorator pattern.)
25 | P a g e
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.
26 | P a g e
"WorkPlace" class, which are related by a many-to-many
associatation "works-in", such that an instance of a
Person is in employee role, when he works-in a job,
where a job is a role of his work place in the situation
when the employee works in it.
27 | P a g e
THE BENEFITS OF INHERITANCE
Some of more important benefits of inheritance are as
follows:
SOFTWARE REUSABILITY
When behavior is inherited from another class, the code
that provides that behavior does not have to be
rewritten. This may seem obvious but the implications
are important. Many programmers spend much of their
time rewriting code they have written many times
before-for example, to search for a pattern in a string or
to insert a new element in to a table. With object
oriented techniques these functions can be written once
and used.
Code sharing:
Code sharing can occur on several levels with
object-oriented techniques. On one level many users or
projects can use the same classes. Another form of
sharing occurs when two or more classes developed by
a single programmer as part of a project inherit from a
single parent class. For example, a set and an array may
both be considered a form of collection. When this
happens, two or more types of objects will share the
code that they inherit. This code needs to be written
only and will contributes only once to the size of the
resulting program.
28 | P a g e
Consistency of interface:
Inheritance two or more classes inherit from the
same super class, we are assured that the behavior they
inherit will be the same in all cases. Thus, it is easier to
guarantee that interfaces to similar objects are in fact
similar and that the user is not presented with a
confusing collection of objects that are almost same but
behave and are interacted with, very differently.
Software components:
Inheritance provides programmer with the ability
to construct reusable software components. The goal is
to permit the development of new and novel application
that nevertheless require little or no actual coding.
Already, several such libraries are commercially
available and we can expect many more specialized
systems to appear in time.
Rapid prototyping:
When software system is constructed largely out
of reusable components, development time can be
concentrated on understanding the new and unusual
portion of the system. Thus, software system can be
generated more quickly and easily, leading to a style of
programming known as rapid prototyping or exploratory
programming. A prototype system is developed user
experiment with it the first; further experimentation
takes place, and so on for several iterations. Such
programming is particularly useful in situations where
the goals and requirement of the system are only
vaguely understood when the project begins.
29 | P a g e
Polymorphism and frameworks:
Software produced conventionally is generally
written from the bottom up although it may be designed
from the top down. That is, the lower-level routines are
written and on top of these slightly higher abstractions
are produced, and on top of these even more
abstraction element are generated. This process is like
building a wall, where every brick must be laid on top of
an already laid brick.
Information hiding:
A programmer who reuses software components
needs only to understand nature of the component and
its interface. It is not necessary for the programmer to
have detailed information concerning matters such as
the techniques used to implement the component. Thus,
the interconnectedness between software systems is
reduced. We earlier identified the interconnected nature
of conventional software as being one of the principle
causes of software complexity.
30 | P a g e
THE COST OF INHERITANCE
The benefit of inheritance in object-oriented
programming are great, almost nothing is without cost
of one sort or another. For this reason we consider the
cost of object oriented programming techniques and in
particular cost of inheritance.
Execution speed:
It is seldom possible for general-purpose software
tools to be as fast as carefully handcrafted systems.
Thus, inherited methods, which must deal with arbitrary
subclasses, are often slower than specialized code.
Program size:
The use of any software library frequently
imposes a size penalty not imposed by system
constructed fir a specific project. Although this expense
31 | P a g e
may be substantial, as memory coast decrease the size
of programs becomes less important. Containing
development coast and producing high quality and
error-free code rapidly are now more important than the
size of programs.
Program complexity:
Object-oriented, programming is often touted
as a solution to software complexity. In fact overuse of
32 | P a g e
inheritance can often simply replace one form if
complexity with another. Understanding the control flow
of a program that uses inheritance may require several
multiple scans up and down the inheritance graph. This
is known as the yo-yo problem.
Uses of Inheritance
The classic examples of an inheritance hierarchy are
borrowed from animal and plant taxonomies. For
example, there could a class corresponding to the
Pinaceae (pine) family of trees. Its subclasses could be
Fir, Spruce, Pine, Hemlock, Tamarack, DouglasFir, and
TrueCedar, corresponding to the various genera that
make up the family. The Pine class might have SoftPine
and HardPine subclasses, with WhitePine, SugarPine,
and BristleconePine as subclasses of SoftPine, and
PonderosaPine, JackPine, MontereyPine, and RedPine as
subclasses of HardPine.
33 | P a g e
able to record the rate of flow. These commonalities can
be encoded once, in a class that the Faucet, Valve, and
WaterPipe classes inherit from. A Faucet can be said to
be a kind of Valve, so perhaps the Faucet class would
inherit most of what it is from Valve, and add very little
of its own.
34 | P a g e
example, that there's an object that would work well in
your program, but you'd like to change one or two things
that it does. You can make the changes in a subclass.
35 | P a g e