INHERITANCE AND EXTENDING CLASSES Java programmers often take advantage of a feature of object-oriented programming called inheritance, which allows programmers to make one class an extension of another existing class. A class that is based on or extended from another class is said to inherit the other class. The class that is extended is called the parent class or the superclass. The class that extends another class is called the child class or the subclass. A class can have many levels of inheritance. For example, consider the following hierarchy: Automobile Honda Accord Civic In the above example, the Accord and Civic classes inherit the Honda class, which inherits the Automobile class. The Automobile class is the superclass of Honda. Honda is the subclass of Automobile and the superclass of Accord and Civic. The Accord and Civic classes are subclasses of Honda. If we were writing software that represented vehicles, you would start by creating a class called Automobile that would describe the features that are common to all types of vehicles (i.e. wheels, a driver, the ability to carry passengers, etc.) A Honda is a type of vehicle that further refines the Automobile class. The Honda class would inherit the Vehicle class and its attributes (i.e. wheels, a driver, the ability to carry passengers, etc.) but would have additional features that differentiate it from other types of vehicles (such as Toyotas, Nissans, Volkswagens, etc.). IMPLEMENTING A SUBCLASS A class that extends another class includes the keyword extends in the class declaration and takes the following form: public class ClassName extends Superclass { Let s say, for example, we have a class named Ball that defines a basic ball and we wanted to create a subclass named BouncingBall that bounces. We would create that subclass as follows: Inheritance and Extending Classes Page 1 of 5
public class BouncingBall extends Ball { public void bounce() { In the above example, the BouncingBall class inherits all the methods and fields of the Ball class. So, if the Ball class has fields named size and diameter, the BouncingBall class has those fields too. Also, if the Ball class has a method named spin(), the BouncingBall class inherits that method as well. Some important things you need to know about extending classes: A subclass inherits all the fields and methods from the superclass. Constructors from the superclass, however, are not inherited. A subclass cannot access methods or fields that are declared as private in the superclass. You can override a method in a subclass by declaring a new method with the same signature. You can add additional methods or fields, private or protected, to a subclass. OVERRIDING METHODS If a subclass declares a method that has the same signature as a public method of the superclass, the subclass version of the method overrides the superclass version of the method. This allows the programmer to modify the behaviour of the superclass to suit the needs of the subclass. Let s say, for example, we have a superclass named Game that has a method named startgame(). The superclass, which doesn t represent any particular game, implements the following method: public class Game { public void startgame() { JOptionPane.showMessageDialog(null, Ready, set, go!, Game, JOptionPane.INFORMATION_MESSAGE); We can declare a subclass called Poker that extends the Game class and overrides the startgame() so that it outputs a different message: public class Poker extends Game { public void startgame() { Inheritance and Extending Classes Page 2 of 5
JOptionPane.showMessageDialog(null, Shuffle up and deal!, Poker, JOptionPane.INFORMATION_MESSAGE); In order to override a method, the following conditions must be met: The class must extend the class that defines the method you want to override. The method must be declared in the superclass with public access. You can t override a private method. The method in the subclass must have the same signature as the method in the superclass (i.e. the name of the method and the parameter types must be the same) USING SUPER IN YOUR SUBCLASSES If you want to refer to a field or method that belongs to a superclass, you can do so by using the super keyword. It works similar to the keyword this, but refers to the instance of a superclass instead of the instance of the current class. In the following example, the outputmessage() method in the AMGreeting class calls the outputmessage() method of its superclass object and then outputs its own message: public class Greeting { public void outputmessage() { System.out.print( Hello! ); public class AMGreeting extends Greeting { public void outputmessage() { super.outputmessage(); System.out.println( Have a great day! ); INHERITANCE AND CONSTRUCTORS When you create an instance of a subclass, the default constructor of the superclass is automatically called before it executes the constructor of the subclass. In the following example, Hello! will be outputted followed by Have a great day! when you create an instance of the AMGreeting class: Inheritance and Extending Classes Page 3 of 5
public class Greeting { public Greeting() { System.out.print( Hello! ); public class AMGreeting extends Greeting { System.out.println( Have a great day! ); You can explicitly call a superclass constructor from a subclass by using the super keyword. Since the default constructor is automatically called, the only time you would want to do this is in the case of a constructor that takes a parameter that you want to set. In the following example, I have called the constructor of the Ball class so that I can specify the diameter of the ball: public class Ball { private double diameter; public Ball(double d) { diameter = d; public class PongBall extends Ball { public PongBall() { super(30); The following are some rules you ll need to keep in mind when working with superclass constructors: If you use super to call the superclass constructor, you must do so in the very first statement in the constructor If you don t explicitly call super, the compiler inserts a call to the default constructor of the superclass. But if the superclass doesn t have a default constructor, it results in a compiletime error. Inheritance and Extending Classes Page 4 of 5
If the superclass is itself a subclass, the constructor for its superclass is called in the same way. Inheritance and Extending Classes Page 5 of 5