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.