Abstract class in Java — Java Technical Interview Questions & Answers
Abstract class in Java
Abstract class in Java
- An abstract class is a partial blueprint. Some methods may be unfinished on purpose. Write abstract class Shape { ... }. You cannot do new Shape() if Shape is abstract.
- An abstract method has no body. Children must implement it, unless the child is also abstract. The abstract class can still have normal methods with real code — that shared code is the point.
- Use it when related classes share code and a common contract: Circle and Rectangle both extend Shape, both implement area(), both reuse colour(). Instantiating an abstract class with new is the beginner mistake.
- Let's take this on the board with one tiny Main class — no extra files. Abstract class in Java — output: 12.56. You cannot write new Shape() — Shape is abstract. new Circle(2) fills in area(). Start from main, go line by line, and stop at each print. That output is the proof for Abstract class.
abstract class Shape
area() ← no body
print() ← real code
│ extends
▼
Circle implements area()Technical Interview Tip: Abstract class = shared code + forced methods for children.
Code Examples for Abstract class in Java
Example
abstract class Shape {
abstract double area();
}
class Circle extends Shape {
double r;
Circle(double r) { this.r = r; }
double area() { return 3.14 * r * r; }
}
public class Main {
public static void main(String[] args) {
Shape s = new Circle(2);
System.out.println(s.area());
}
}Abstract class in Java — output: 12.56. You cannot write new Shape() — Shape is abstract. new Circle(2) fills in area().
How to Explain Abstract class in Java to an Interviewer
Can you do new Shape() if Shape is abstract?
No
What is an abstract method?
No body Child must implement
Can an abstract class have concrete methods?
Yes That is shared code