Search Logic Blocks

Friday, September 11, 2020

Java: What are Abstract classes?

What are the Abstract Methods?
Abstract methods are methods, which are declared as abstract by using keyword abstract. Abstract methods don't have any implementation and need to be overridden by subclasses. All subclasses must implement the abstract method of super class. When a method is declared as an abstract method, the class must be declared as abstract too.

What is an Abstract Class?
Abstract class is a class which is declared as abstract by using the keyword abstract, and may or may not have abstract methods. An abstract class can not be instantiated, means abstract classes can't have instances, they are to be subclassed / extended necessarily.

Let's review our shape class in earlier posts. In the class definition, we have one method called area() which is required by all the subclasses definitely, but there is no such implementation to calculate area in shape class itself. So, we can convert method area() to an abstract method and also the shape class into an abstract class. Here is the code how we can write an abstract class shape with abstract method area() -
public abstract 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 abstract double area();


public void setColor(String color) {
this.borderColor = color;
}
}
And here is the subclass Triangle of shape class and ShapesTest class to show demo of an abstract class -
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);
}
}
So, to use an abstract class, it must be extended, as the abstract class can not be instantiated. And all the abstract methods must be implemented in the subclass.

We have already learnt about Polymorphism. Inheritance, method overloading, method overriding and abstract classes, all are used to support polymorphism. Code can be found here on Github.

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.