The keyword super refers to the superclass and can be used to invoke the superclass’s methods and constructors. A subclass inherits accessible data fields and methods from its superclass. Does it inherit constructors? Can the superclass’s constructors be invoked from a subclass? This section addresses these questions and their ramifications.
The this Reference, introduced the use of the keyword this to reference the calling object. The keyword super refers to the superclass of the class in which super appears. It can be used in two ways:
- To call a superclass constructor.
- To call a superclass method.
Calling Superclass Constructors
A constructor is used to construct an instance of a class. Unlike properties and methods, the constructors of a superclass are not inherited by a subclass. They can only be invoked from the constructors of the subclasses using the keyword super.
The syntax to call a superclass’s constructor is:
super(), or super(parameters);
The statement super() invokes the no-arg constructor of its superclass, and the statement super(arguments) invokes the superclass constructor that matches the arguments. The statement super() or super(arguments) must be the first statement of the subclass’s constructor; this is the only way to explicitly invoke a superclass constructor. For example, the constructor in lines 12–16 CircleFromSimpleGeometricObject.java, here, can be replaced by the following code:
public CircleFromSimpleGeometricObject(
double radius, String color, boolean filled) {
super(color, filled);
this.radius = radius;
}
You must use the keyword super to call the superclass constructor, and the call must be the first statement in the constructor. Invoking a superclass constructor’s name in a subclass causes a syntax error.
Constructor Chaining
A constructor may invoke an overloaded constructor or its superclass constructor. If neither is invoked explicitly, the compiler automatically puts super() as the first statement in the constructor. For example:
In any case, constructing an instance of a class invokes the constructors of all the superclasses along the inheritance chain. When constructing an object of a subclass, the subclass constructor first invokes its superclass constructor before performing its own tasks. If the superclass
is derived from another class, the superclass constructor invokes its parent-class constructor before performing its own tasks. This process continues until the last constructor along the inheritance hierarchy is called. This is called constructor chaining.
Consider the following code:
public class Faculty extends Employee {
public static void main(String[] args) {
new Faculty();
}
public Faculty() {
System.out.println("(4) Performs Faculty's tasks");
}
}
class Employee extends Person {
public Employee() {
this("(2) Invoke Employee's overloaded constructor");
System.out.println("(3) Performs Employee's tasks ");
}
public Employee(String s) {
System.out.println(s);
}
}
class Person {
public Person() {
System.out.println("(1) Performs Person's tasks");
}
}
(1) Performs Person's tasks
(2) Invoke Employee's overloaded constructor
(3) Performs Employee's tasks
(4) Performs Faculty's tasks
The program produces the preceding output. Why? Let us discuss the reason. In line 3, new Faculty() invokes Faculty’s no-arg constructor. Since Faculty is a subclass of Employee, Employee’s no-arg constructor is invoked before any statements in Faculty’s constructor are executed. Employee’s no-arg constructor invokes Employee’s second constructor (line 13). Since Employee is a subclass of Person, Person’s no-arg constructor is invoked before any statements in Employee’s second constructor are executed. This process is illustrated in the following figure.
If a class is designed to be extended, it is better to provide a no-arg constructor to avoid programming errors. Consider the following code:
public class Apple extends Fruit {
}
class Fruit {
public Fruit(String name) {
System.out.println("Fruit's constructor is invoked");
}
}
Since no constructor is explicitly defined in Apple, Apple’s default no-arg constructor is defined implicitly. Since Apple is a subclass of Fruit, Apple’s default constructor automatically invokes Fruit’s no-arg constructor. However, Fruit does not have a no-arg constructor, because Fruit has an explicit constructor defined. Therefore, the program cannot be compiled. If possible, you should provide a no-arg constructor for every class to make the class
easy to extend and to avoid errors.
Calling Superclass Methods
The keyword super can also be used to reference a method other than the constructor in the superclass. The syntax is:
super.method(parameters);
You could rewrite the printCircle() method in the Circle class as follows:
public void printCircle() {
System.out.println("The circle is created " +
super.getDateCreated() + " and the radius is " + radius);
}
It is not necessary to put super before getDateCreated() in this case, however, because getDateCreated is a method in the GeometricObject class and is inherited by the Circle class. Nevertheless, in some cases, as shown in the next section, the keyword super is needed.
Top comments (0)