03-05-2012, 03:39 PM
Inheritance and Class Hierarchies
ch03-inheritance.ppt (Size: 423 KB / Downloads: 230)
Introduction to Inheritance and Class Hierarchies
Popularity of OOP is that it enables programmers to reuse previously written code saved as classes
All Java classes are arranged in a hierarchy, starting with Object, which is the superclass of all Java classes
Inheritance in OOP is analogous to inheritance in humans
Inheritance and hierarchical organization allow you to capture the idea that one thing may be a refinement or extension of another
Is-a Versus Has-a Relationships
One misuse of inheritance is confusing the has-a relationship with the is-a relationship
The has-a relationship means that one class has the second class as an attribute
We can combine is-a and has-a relationships
The keyword extends specifies that one class is a subclass of another
Initializing Data Fields in a Subclass and the No-Parameter Constructor
Private data fields belonging to a base class must be initialized by invoking the base class’s constructor with the appropriate parameters
If the execution of any constructor in a subclass does not invoke a superclass constructor, Java automatically invokes the no-parameter constructor for the superclass
Initializes that part of the object inherited from the superclass before the subclass starts to initialize its part of the object