Lesson: Inheritance

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

Understand object inheritance and the concept of extending objects.


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 inherit the fields and methods of some other existing object. We can say our new object inherits or extends the aspects of the base object.

We inherit or extend an object into a new object by using the extends keyword on the 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. Since NewClass is a sub type 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 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 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 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:

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.


Lesson navigation: