As the name suggest, an Abstract class is a conceptual idea of an object but it doesn’t make sense to create objects from it as it may have methods that are not yet implemented.
When you know what an object does, but if the implementation can only be decided in the subclass then we declare the class and those unclear methods as abstract.
So, we can conclude that an abstract class is class with abstract methods. And, an abstract method is an empty method without implementation.
package com.techstackjournal.shapes;
public abstract class Shape {
public abstract void draw();
}
A Shape is a non-representational object, it doesn’t have any form, so it would be correct to declare it as an abstract class.
Similarly, in Shape class we cannot write code to draw as we don’t know the shape of it. Each subclass of Shape, like Circle, Rectangle may draw the shape differently, so, it is correct to declare the draw method also as abstract within Shape class.
Also, please note that we need to declare the abstract method without body.
To declare a class as abstract, we do not need the methods to be abstract, we can force developers to inherit it the .
package com.techstackjournal.shapes;
public class Circle extends Shape {
public void draw() {
System.out.println("Drawing Circle");
}
}
If a class extends an abstract class it must provide the implementation of all the abstract methods of the super class, else the subclass also needs to be marked as abstract class.
If a subclass implements all abstract methods, we call it as concrete class as it provides concrete implementation to the abstract methods.
In the above example, Circle class extends Shape class and provides implementation to draw method, so Circle class is a concrete subclass.