Inheritance is at the heart of Object Orientation, it means basing a new class on an existing class. The use term inheritance comes from the way it is used in nature, as in “I inherited my fathers large nose”, or “I inherited my grandfathers huge estate and massive 90 bedroom stately home”. The idea is that rather than creating functionality “from scratch” each time you can get it from an existing example. The practical benefit is that a programmer has less work to do and the code that is used will have been developed and tested over a longer period of time, which should result in more reliable code. Another benefit of Inheritance is that you can inherit most of the functionality of a parent class but modify some of that behaviour, again all without necessarily having access to the source code of the parent class.

The relationship between classes that inherit from each other can be described in several different ways, some of which are not obvious. Thus a class is said to be a child of another or that it “inherits from another” . A class can also be described as “a base class”, a slightly confusing term as base generally means at the bottom of, and if you look at an inheritance tree, you might think that the most “recent” class would be at the bottom and would thus be the base class. In OO terms the base class is the one further up the inheritance tree, i.e. The grandparent or great grandparent class. Where one class is a child of another it is sometimes described as being a subtype of the other.

The keyword for inheritance is “extends”, thus one class extends another. Here is an example

class Animal{
	private String sName="animal";
	public String	getName(){
		return sName;


public class Cat extends Animal{
	public static void main(String argv[]){
	 Animal a = new Animal();

Note how the class Cat has no method called getName but because it extends the class Animal it has access to that functionality. A class can only extend one other class, as Java implements only single inheritance (i.e. One parent class, unlike humans who have a default of two parents).

Going back to the example of the Animal class as a base type, note that there is no concrete example in the real world of a type of Animal that is just an Animal. All animals are some other type, be that a Cat, Dog, bird or Cow. In the same way a base class in Java will often be of a type that you may never create an instance of, but is only used as the base class, or foundation of another.

The hierarchy of inheritance can be as deep as you like, i.e you can have a child with a parent, grandparent, great grandparent great, great, grandparent etc etc (you get the picture). However in a similar way that hierarchies of disk directories usually only go to a few levels it is more common to only have relatively shallow hierarchies. You can see demonstrations of typical inheritance trees if you look at the API (Application Programmer Interface) documentation of the Java class libraries. For example if you take a look at any of the Collection classes you will see what the parents and grandparents of a class are, and also what other classes inherit from each class.

Last modified: Thursday, 24 July 2014, 2:54 PM