The Pitfalls of Inheritance
by: Samudra Gupta
In Object Oriented Programming, inheritance is a commonly used
mechanism to model the relationship between two types. However,
modelling such relationship without realizing the impact on the
overall application may result in unexpected problems. In this
article, I will try to present some internal details of
inheritance mechanism based on the Java language and highlight
some of the problems that a developers should be aware of when
using inheritance of types.
Inheritance Defined…
One of the first questions that we should answer is what do we
mean by inheritance. The inheritance is a modelling technique of
expressing the relationship between a generalized type and a
specialized type. This in essence is a way of expressing IS A
relationship. For example, we can say Car IS A Vehicle. In such
situations, we will say Car inherits from Vehicle. In the world
of Java language, this inheritance relationship is expressed
with the following semantics.
class Car extends Vehicle {
In this model, Car is a subclass of the Vehicle super class. Now
the next question is what we inherit. By default, the subclass
inherits all the interfaces and the implementations from the
super class. Note that, the use of the word "interface" in this
context does not refer to the interface type specific to the
Java language. In OO, the "interface" is a function/method that
a particular class exposes to the external world. Coming
specific to Java, the inheritance relationship guarantees the
following:
- The sub class inherits all the public methods and
the implementation in the super class.
- The sub class inherits all the protected methods
and their implementation in the super class.
- The sub class also inherits all the public and
protected member variables from the super class.
- However, constructors are not part of this
inheritance model. We shall also see later the invocation
relationship of constructors that exists between a sub class
and its immediate super class.
These consequences of inheritance also imply the following
things that are vital to how we can use a super class and any
sub class of it. In general, when you model and implement an
inheritance relationship, you guarantee the following:
- The sub class is capable of accepting all the
messages that the super class accepts.
- The sub class can replace the super class anywhere
the super class is called for without affecting the final outcome.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.
|