J2EEOnline J2EE 


Java Fundamentals  «Prev  Next»
Lesson 5Putting inheritance to work
ObjectiveBuild a hierarchy of classes using inheritance.

Build Hierarchy of Classes using Inheritance using Java

Class inheritance

For the record, the parent/child OOP metaphor is not just something I cooked up to make the course more entertaining. In truth, inheritance works very much like biological inheritance. In biological inheritance, a child inherits many of the physical and personality traits of its two parents. However, the child also develops its own unique traits that are present right alongside the inherited traits. This blend of old and new is what makes us all so unique as individual people. Some of us are more unique than others, but you get the idea. Although Java classes aren't nearly as unique as people, they do reap the same rewards from inheritance. One big difference is that only one class can be the parent of a child class in Java, which I suppose, makes Java an asexual programming language. I could be carrying this analogy a bit too far.

As you learned earlier in the module, inheritance involves creating a new class with the characteristics of an existing class, along with additional characteristics unique to the new class. The Object class[1] forms the root of the Java class hierarchy, which means that all classes in Java derive either directly or indirectly from it.

Class or an Object?

The concept of a class named Object might seem a little strange at first. As you learn a little later in the module, an object is just an instance of a class. The Object class is a very special class, however, because it serves as the base class for all Java classes.
If you were to make a Java family tree, the Object class would be at the top as the great-great-great grandparent of all classes.
This includes the classes in the Java API.
Inheritance in use
Example of Java Inheritance

This figure shows how inheritance is used in Java to form a hierarchy of classes. Some classes don't explicitly declare a parent class, in which case they are implicitly derived from Object. The Lion class in the previous lesson is a good example of a class that implicitly derives from Object.
The extends keyword[2] is used to identify a class as the child of a parent class. The following code shows how the Lion class might explicitly derive from a class other than the Object class:
class Lion extends Predator {
  int energy;
  int aggression;
  boolean hungry;
  Color color;
  
  public Lion() {
   Lion(100, 15, false, Color.yellow);
  }
  
  public Lion(int e, int a, boolean h, Color c) {
   energy = e;
   aggression = a;
   hungry = h;
   color = c;
  }
} 

Predator is parent of Lion

The Predator class is identified as the parent of the Lion class, which means that the Lion class inherits all of the member variables and methods defined in the Predator class.
Keep in mind that at some point the Object class still serves as an indirect parent of the Lion class since all classes ultimately derive from Object. In this case the Predator class might derive directly from Object.
[1] Object class: The >Object< class is the class that forms the root of the Java class hierarchy. The >Object< class serves as the basis for all Java classes, including the classes in the Java API.
[2] Keywords: Keywords are special identifiers set aside as Java programming constructs. Typical keywords in Java include <tt>int</tt>, <tt>class</tt>, <tt>for</tt>, <tt>while</tt>, <tt>new</tt>, <tt>private</tt>, and <tt>switch</tt>.