Search Logic Blocks

Saturday, August 22, 2020

Java: How to access superclass members

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 -

4.0
6.0
Yellow
Green
Pink

All the code can be found at Github Link.

Wednesday, August 19, 2020

Java: How to access superclass constructors

As we have already discussed about inheritance earlier and we have also learnt about how to override the superclass methods in subclasses. Superclass is a common class, subclass can access all non-private members from the superclass and can also add new own members. Superclass non-private member variables can be accessed directly, but superclass constructors and non-private member methods can be accessed by using a keyword super. 

How to access Superclass constructor
If the subclass doesn't have any constructor and superclass has a constructor with no arguments, by default the superclass constructor method is invoked. Let's take the example Shape, Triangle and ShapesTest from earlier post and modify by adding two constructor methods -
public class Shape {
final static double PI = Math.PI;
double width = 0;
double height = 0;

// Superclass constructors
Shape() {
this.width = 5;
this.height = 8;
}

Shape(int width, int 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 class Triangle extends Shape {
// 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 triangle = new Triangle();

System.out.println(triangle.whatShape());
System.out.println(triangle.width);
System.out.println(triangle.height);
}
}
In the above example, Shape class has two constructors and Triangle class doesn't have any constructor. But when an instance of Triangle class is created, superclass constructor is automatically called. Here is the output for above code -

Triangle
5.0
8.0

Let's add one new member variable and one constructor in subclass Triangle with no arguments. And also print the new member variable value in the ShapesTest class. We have added new member variable as fillColor and Triangle class constructor initializes fillColor as "Red" -
public class Triangle extends Shape {
String fillColor;
// Triangle Constructor with no arguments
Triangle() {
this.fillColor = "Red";
}
// 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 triangle = new Triangle();

System.out.println(triangle.whatShape());
System.out.println(triangle.width);
System.out.println(triangle.height);
System.out.println(triangle.fillColor);
}
}
In the above example, we have added one new member variable fillColor in subclass Triangle. When the new instance of Triangle class is created, superclass constructor is called automatically following by the subclass constructor with no arguments. Here is the output of above code -

Triangle
5.0
8.0
Red

Let's create another constructor in subclass with one argument to set value of the subclass member variable fillColor dynamically. Still, the superclass constructor (with no arguments) is called implicitly before calling the subclass constructor, and superclass member variables are initialized. Here is the code -
public class Triangle extends Shape {
String fillColor;
// Triangle Constructor with no arguments
Triangle() {
this.fillColor = "Red";
}

// Triangle Constructor with one argument
Triangle(String fillColor) {
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 triangle1 = new Triangle();

Triangle triangle2 = new Triangle("Blue");

System.out.println(triangle1.whatShape());
System.out.println(triangle1.width);
System.out.println(triangle1.height);
System.out.println(triangle1.fillColor);

System.out.println();
System.out.println(triangle2.whatShape());
System.out.println(triangle2.width);
System.out.println(triangle2.height);
System.out.println(triangle2.fillColor);
}
}
And the output is -

Triangle
5.0
8.0
Red

Triangle
5.0
8.0
Blue

But, if you are not sure if the superclass constructor is called, you can call it by using super() method. It will give the same result as above. Here is the code with subclass constructor with call to superclass constructor using super() method (note that superclass constructor call statement should be the first statement in the subclass constructor) -
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) {
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;
}
}
Only subclass Triangle is changed, and the output is same above -

Triangle
5.0
8.0
Red

Triangle
5.0
8.0
Blue

Let's add one more constructor in subclass which takes three arguments and calls superclass constructor method with two arguments. Here we will use again super() method but with two arguments. Here is the code - 
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 triangle1 = new Triangle();

Triangle triangle2 = new Triangle("Blue");

Triangle triangle3 = new Triangle(4, 6, "Purple");

System.out.println(triangle1.whatShape());
System.out.println(triangle1.width);
System.out.println(triangle1.height);
System.out.println(triangle1.fillColor);

System.out.println();
System.out.println(triangle2.whatShape());
System.out.println(triangle2.width);
System.out.println(triangle2.height);
System.out.println(triangle2.fillColor);

System.out.println();
System.out.println(triangle3.whatShape());
System.out.println(triangle3.width);
System.out.println(triangle3.height);
System.out.println(triangle3.fillColor);
}
}
Here is the output of above example -

Triangle
5.0
8.0
Red

Triangle
5.0
8.0
Blue

Triangle
4.0
6.0
Purple

Note that the super() method call is the first statement in subclass constructors. You can find code at Github Link.