A super
keyword refers to the super class. It can be used to invoke super class methods and instance variables. A super()
constructor call is used for calling super class constructors.
Invoking super class methods
When we override any of the super class method, the behavior of super class method will be lost by subclass method behavior. But if you would like to make use of super class method, super keyword will comes to our rescue. You can invoke any visible method of super class from any method within subclass using super
keyword.
package com.techstackjournal.superkeyword;
public class Shape {
public void draw() {
System.out.println("Preparing the canvas...");
}
public void paint() {
System.out.println("Painting the canvas...");
}
}
package com.techstackjournal.superkeyword;
public class Rectangle extends Shape {
public void draw() {
super.draw();
System.out.println("Drawing Rectangle...");
super.paint();
}
public void paint() {
super.draw();
System.out.println("Painting Rectangle...");
super.paint();
}
}
package com.techstackjournal.superkeyword;
public class Application {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.draw();
rectangle.paint();
}
}
Preparing the canvas...
Drawing Rectangle...
Painting the canvas...
Preparing the canvas...
Painting Rectangle...
Painting the canvas...
Invoking Super Class Constructors
- Consider that in your class design you have a super class with a constructor that initialize common instance variables and perform some logical operations.
- When you create a subclass you should not rewrite that code again as it will duplicate the logic.
- Instead, you should call the super class constructor to execute that logic
- Using
super()
constructor call we can invoke super class constructor - Each subclass contains an implicit
super()
no-arg constructor call within its constructor inserted by compiler - But if you want to invoke a with-arg constructor you can use
super()
constructor call explicitly - The
super()
constructor call must be the first line of any constructor - If super class define with-arg constructors only, then a subclass must explicitly call anyone of those constructors
- Because, compiler cannot insert the default constructor call as it is not present in the super class
- You can also have
this()
as the first line of constructor call, which basically calls the constructor from the same class - When you have
this()
call you cannot insertsuper()
call in the constructor
package com.techstackjournal.superconstructor;
public class Shape {
public double x, y;
public Shape(double x, double y) {
System.out.println("Initializing coordinates...");
this.x = x;
this.y = y;
}
}
package com.techstackjournal.superconstructor;
public class Rectangle extends Shape {
public double width, height;
public Rectangle(double x, double y, double width, double height) {
super(x, y);
System.out.println("Initializing width and height...");
this.width = width;
this.height = height;
}
public String toString() {
return "Rectangle [width=" + width + ", height=" + height + ", x=" + x + ", y=" + y + "]";
}
}
Here, we are reusing the super class constructor by invoking it using super()
constructor call.
package com.techstackjournal.superconstructor;
public class Application {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(10, 10, 100, 200);
System.out.println(rectangle);
}
}
Initializing coordinates...
Initializing width and height...
Rectangle [width=100.0, height=200.0, x=10.0, y=10.0]
Constructor Chaining
- We’ve learnt that Constructor of a class will get executed when you create an instance of a class using new keyword.
- But, if the class is in the chain of inheritance, constructor of the super class which is on top of the chain will get executed first.
- Then other super class constructors in the chain from top to bottom will get executed.
- Finally, the constructor of the class whose instance is being created will continue to execute.
package com.techstackjournal.constructor_chaining;
public class Alpha {
public Alpha() {
System.out.println("In Alpha constructor");
}
}
package com.techstackjournal.constructor_chaining;
public class Beta extends Alpha {
public Beta() {
System.out.println("In Beta constructor");
}
}
package com.techstackjournal.constructor_chaining;
public class Gamma extends Beta {
public Gamma() {
System.out.println("In Gamma constructor");
}
}
package com.techstackjournal.constructor_chaining;
public class Application {
public static void main(String[] args) {
new Gamma();
}
}
In Alpha constructor
In Beta constructor
In Gamma constructor