Header javaperspective.com
JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.4. Inheritance

4.4. Inheritance
Last updated: 23 January 2013.

In the Java language, if a class C2 has the same properties (fields) and behaviour (methods) as a class C1 while adding its own features, C2 inherits from C1. C1 is called the super class and C2 is the subclass. For example, a class named Helicopter can be declared as a subclass of the class Aircraft that I have used in the previous tutorial. Likewise, a class named Plane can be a subclass of the class Aircraft:

picture illustrating the Java inheritance concept


There is a is A relationship between a subclass and its super class. A helicopter is an aircraft but an aircraft is not a helicopter. The class Helicopter inherits all the fields and methods of the class Aircraft and additionally has its own specific features like its main rotor blade size, its tail rotor blade size and a hovering method. The keyword extends is used by the class Helicopter to inherit from the class Aircraft:

public class Helicopter extends Aircraft {

   
float mainRotorBladeSize;
   
float tailRotorBladeSize;

   
public void hovering(){
         
// The code for hovering goes here
   
}

}

A subclass can only have one super class. A super class can have an unlimited number of subclasses. A super class can itself be a subclass of another class. In the Java language, every class is a subclass of the predefined class Object. In other words, the class Object is the mother of all classes. You will learn more about the predefined classes of the Java language in the tutorial The Java API.

A subclass can change the behaviour of the super class by overriding the methods that are not declared final. For example, the class Plane shown below overrides the method land of its super class by providing a method with the same signature. The class Plane also has a specific property (wingSpan) and a specific method (gliding):

public class Plane extends Aircraft {

   
float wingSpan;

   
public void gliding(){
         
// The code for gliding goes here
   
}

   
public boolean land(){
         
// The new code to land goes here
   
}

}

The constructors of a super class are not inherited by subclasses. Thus, on the one hand, if a subclass does not have a constructor of its own, the constructor of its super class is called when a subclass object is created. On the other hand, if a subclass declares a constructor of its own, the constructor of the super class is called before executing the code of the subclass constructor:

public class Plane extends Aircraft {

   
float wingSpan;

   
public Plane(){
         
// This code will be executed after the call to the contructor of the super class
          //...
   
}

   
public void gliding(){
         
// The code for gliding goes here
   
}

   
public boolean land(){
         
// The new code to land goes here
   
}

}

If a super class has several constructors, it is the constructor which does not take any parameters that is called when a new subclass object is created. If you want to execute a super class constructor with parameters, you have to use the keyword super as follows:

public class Plane extends Aircraft {

   
float wingSpan;

   
public Plane(){
         
super(58.17F, 12.7F, 257, 17650.5D, false);
         
// some other statements...
          //...
   
}

   
public void gliding(){
         
// The code for gliding goes here
   
}

   
public boolean land(){
         
// The new code to land goes here
   
}

}

The call to super must be the first statement in the subclass constructor. Note that a call to super without passing in any parameters simply calls the constructor of the super class which does not take any parameters. But as I said before, that call is implicit. The keyword super can also be used to access fields and methods of the super class. For example, the class Plane overrides the method land but for some reason, you may want to call the method land provided by the super class. To illustrate such a case, let's write a method called landLikeTheSuperClass:

public class Plane extends Aircraft {

   
float wingSpan;

   
public Plane(){
         
super(58.17F, 12.7F, 257, 17650.5D, false);
         
// some other statements...
          //...
   
}

   
public void gliding(){
         
// The code for gliding goes here
   
}

   
public boolean land(){
         
// The new code to land goes here
   
}

   
public boolean landLikeTheSuperClass(){
         
return (super.land());
   
}

}

In the above example, super.land() is a call to the method land provided by the super class Aircraft.

A class can be declared final as in the example shown below:

public final class Aircraft {

}

If a class is declared final, all of its methods are implicitly declared final. A class that is declared final cannot have any subclasses. Likewise, a method that is declared final cannot be overridden. For performance reasons, it is recommended to declare final the classes that are not supposed to be subclassed. Such classes are called immutable classes. If a class can have subclasses (it is not declared final), you can declare final all the methods that you do not want to be overridden by subclasses.


You are here :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.4. Inheritance
Next tutorial :  JavaPerspective.com  >   Beginner Tutorials  >   4. Object-Oriented Concepts  >   4.5. Abstract classes

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7