Lesson: Inheritance

Overview: 
Explore the Inheritance feature of Java's object oriented design.
Objectives: 

Understand object inheritance and the concept of extending objects.

Content: 

In our first unit on objects we learned how objects can encapsulate fields and methods creating a custom data type we can use to model the actual objects in the problem we are trying to solve. These objects make it easy to describe and work with our data and facilitate reuse of code. In this unit we are going to explore some of the more powerful features of Java's object oriented design.

The first advanced object feature we will discuss is inheritance. Inheritance simply means that when we design a new object, we can include the fields and methods of some other existing object. We can say our new object inherits or extends the characteristics of the base object.

We inherit or extend an object into a new object by using the extends keyword on the new objects class definition:

When we do this, NewClass is said to be a subclass (or child) of OldClass. OldClass is said to be the superclass (or parent) of NewClass. OldClass may also be called the base class of NewClass. A class may extend only one other class. If a class does not explicitly extend another class, then it automatically extends the built-in Java class Object. Classes may extend classes that extend other classes creating a class hierarchy. All class hierarchies have the Object class at the base. Since NewClass is a subclass of OldClass, the NewClass object can be used anywhere an OldClass object is expected.

NewClass will have whatever fields and methods you write for it and it will also have all of the accessible (public) fields and methods of OldClass (except for the constructors). This idea of inheritance allows us to build new objects reusing existing code by extending or specializing our classes into more specific or customized objects for modeling more specific object data and behaviors.

Lets look at an example. We will start with a class called Animal. It will have some fields and methods common to all animals. We will extend Animal with the more specialized classes Dog and Cat. These classes will have fields and methods that apply only to them but will also have the fields and methods of class Animal:

The Dog and Cat class constructors show how to use the super keyword in a subclass to access constructors and members of the superclass. So you see that the Dog and Cat classes do not have a name member defined in them but the resulting classes do have a name member since it is defined in the parent or super class.

Here is the example above in CodingGround.  Fix the compile error and see the code in action.

One aspect of inheritance is that you can use a subclass any place that the super class is required. In our example, you can use a Cat or a Dog object any place an Animal object is required. If you add this method to the Animal class above:

Then you can to this in the main method:

This example also shows something useful. The Java Object class has a method called getClass(). When called from any class it returns information about that calling class. Since Animal is automatically a subclass of Object, in the println() statement, we call animal.getClass().getName() to get the name of the calling class. You might expect this statement to print out "Animal" for the class, but since we passed in instances of Dog and Cat in place of Animal, you get the name of those classes.

Modify the CodingGroup example and add an animal of your choice and test.

Object hierarchies are very powerful and used extensively in robotics API libraries. Extending classes allows you to build complex classes out of simpler classes and reuse code in classes rather than duplicating that code in other classes.

Here is a video about inheritance. Here is a detailed discussion of inheritance. Here is more information about the super keyword.

 

Navigation: