We have already seen how to call superclass constructors in the last post - How to access superclass constructors. Subclass instances / objects can access all the non-private superclass members (variables and methods) directly. But if the methods are overridden, the subclass instances call only the overridden subclass methods. But if the program needs to call superclass method, subclass methods can call them by using a keyword super -
public class Shape {
final static double PI = Math.PI;
double width = 0;
double height = 0;
String borderColor = "Blue";
// Superclass constructors
Shape() {
this.width = 5;
this.height = 8;
}
Shape(double width, double height) {
this.width = width;
this.height = height;
}
// Returns the text telling what shape it is
public String whatShape() {
return "";
}
// Returns the area of the shape
public double area() {
return 0.0;
}
public void setColor(String color) {
this.borderColor = color;
}
}
public class Triangle extends Shape {
String fillColor;
// Triangle Constructor with no arguments
Triangle() {
super();
this.fillColor = "Red";
}
// Triangle Constructor with one argument
Triangle(String fillColor) {
super();
this.fillColor = fillColor;
}
// Triangle Constructor with three arguments
Triangle(double width, double height, String fillColor) {
super(width, height);
this.fillColor = fillColor;
}
// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}
// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((height * width / 2) * 100) / 100.0;
}
}public class ShapesTest {
public static void main(String args[]) {
// Created a triangle shape
Triangle t = new Triangle(4, 6, "Yellow");
System.out.println(t.width);
System.out.println(t.height);
System.out.println(t.fillColor);
System.out.println(t.borderColor);
}
}
In the above example, a new method setColor() is added to set the Shape class's bordercolor variable. And subclass Triangle hasn't overridden the method setColor(). The Triangle class instance calls the method setColor(), Triangle class doesn't have any method named as setColor(). So, the setColor() method from the superclass is called automatically. Here is the output -
4.0
6.0
Yellow
Blue
Overriding Methods
Let's override the method setColor() and then call the method from the subclass instance. In this case, the subclass version of the method setColor() is called. Here is the code and output.
public class Triangle extends Shape {
String fillColor;
// Triangle Constructor with no arguments
Triangle() {
super();
this.fillColor = "Red";
}
// Triangle Constructor with one argument
Triangle(String fillColor) {
super();
this.fillColor = fillColor;
}
// Triangle Constructor with three arguments
Triangle(double width, double height, String fillColor) {
super(width, height);
this.fillColor = fillColor;
}
// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}
// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((height * width / 2) * 100) / 100.0;
}
public void setColor(String color) {
this.fillColor = color;
}
}
public class ShapesTest {
public static void main(String args[]) {
// Created a triangle shape
Triangle t = new Triangle(4, 6, "Yellow");
System.out.println(t.width);
System.out.println(t.height);
System.out.println(t.fillColor);
t.setColor("Green");
System.out.println(t.fillColor);
System.out.println(t.borderColor);
}
}
4.0
6.0
Yellow
Green
Blue
In above example, setColor() method in the subclass method is changing the fillcolor, not bordercolor. So, fillColor is Green and borderColor is same as blue.
Call superclass method from the overridden method
To call the superclass method from the subclass method, the keyword super is used. Here in the example, the following statement calls the superclass method setColor(). And the output is changed accordingly -
public class Triangle extends Shape {
String fillColor;
// Triangle Constructor with no arguments
Triangle() {
super();
this.fillColor = "Red";
}
// Triangle Constructor with one argument
Triangle(String fillColor) {
super();
this.fillColor = fillColor;
}
// Triangle Constructor with three arguments
Triangle(double width, double height, String fillColor) {
super(width, height);
this.fillColor = fillColor;
}
// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}
// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((height * width / 2) * 100) / 100.0;
}
public void setColor(String color) {
super.setColor(color);
this.fillColor = color;
}
}
4.0
6.0
Yellow
Green
Green
Superclass method can be called from any subclass method
To call the superclass methods, the method does not need to be overridden. Here in the following example, a new method setDiffColor() is added, it passes two parameters - fillcolor and bordercolor. The superclass method is called to set bordercolor.
public class Triangle extends Shape {
String fillColor;
// Triangle Constructor with no arguments
Triangle() {
super();
this.fillColor = "Red";
}
// Triangle Constructor with one argument
Triangle(String fillColor) {
super();
this.fillColor = fillColor;
}
// Triangle Constructor with three arguments
Triangle(double width, double height, String fillColor) {
super(width, height);
this.fillColor = fillColor;
}
// Returns the text telling what shape it is
public String whatShape() {
return "Triangle";
}
// Returns the area of the shape - can access parent's declared variables
public double area() {
return Math.round((height * width / 2) * 100) / 100.0;
}
public void setColor(String color) {
super.setColor(color);
this.fillColor = color;
}
public void setDiffColor(String fillcolor, String bordercolor) {
this.fillColor = fillcolor;
super.setColor(bordercolor);
}
}
public class ShapesTest {
public static void main(String args[]) {
// Created a triangle shape
Triangle t = new Triangle(4, 6, "Yellow");
System.out.println(t.width);
System.out.println(t.height);
System.out.println(t.fillColor);
t.setDiffColor("Green","Pink");
System.out.println(t.fillColor);
System.out.println(t.borderColor);
}
}
Here is the output -