Java Reference
In-Depth Information
Chapter 16
Using Inheritance
Inheritance and Data Abstraction
Inscience,inheritanceisa mechanismfororganizingknowledgeintohier-
archies. In object-oriented computer languages, such as Java, inheritance
is implemented by means of a class hierarchy in which one class, usually
calledthederivedclass,inheritsthedatamembersandthefunctionalityof
anotherone,usuallycalledthebaseclassorthesuperclass.Inshort,inheri-
tancemakesitpossibleforanobjectofasubclasstocontaintheattributes
and properties of the superclass. This mechanism makes it easier to de-
velop software by avoiding duplication and improving the organization of
theknowledgebase.Italsomakesprogramsmorereliableandeasiertoex-
tend and to repair. In this chapter we look at class inheritance in Java code
and how inheritance promotes a higher level of abstraction.
Java Inheritance
Inheritance makes it possible to reuse the data members and the process-
ing capabilites of a class. From a programmer's view, the advantages of us-
ing class inheritance are that it fosters code reusability and simplifies
coding. Java inheritance is achieved by making a class extend another
class, for example:
class Bird extends Pet
{
...
}
In this case the subclass is Bird and the superclass is Pet. Bird inherits
all the public members of the superclass Pet. But Bird is not limited to the
attributes and methods of Pet, since it can have attributes and methods of
its own.
Search WWH ::




Custom Search