Java Abstract Classes
In this page:
What is an Abstract Class?
An abstract class is declared with the abstract keyword and can never be instantiated directly with new — it exists purely to serve as a partial blueprint that concrete subclasses build on top of.
Example: What is an Abstract Class?
abstract class Shape {
}
public class Main {
public static void main(String[] args) {
// Shape s = new Shape(); // would not compile: cannot instantiate abstract class
System.out.println("Abstract class cannot be instantiated");
}
}
Login to try C/C++/Java/PHP code in the editor
Defining Abstract Methods
An abstract method is declared with no method body at all, just a signature ending in a semicolon; it exists to force every concrete subclass to supply its own specific implementation, guaranteeing that method will exist somewhere down the hierarchy.
Example: Defining Abstract Methods
abstract class Shape {
abstract double area(); // no body, just a signature
}
public class Main {
public static void main(String[] args) {
System.out.println("Abstract method declared");
}
}
Login to try C/C++/Java/PHP code in the editor
Implementing Abstract Methods
Any concrete (non-abstract) subclass that extends an abstract class must implement every one of its inherited abstract methods, or the subclass itself has to be declared abstract too, deferring the obligation further down the chain.
Example: Implementing Abstract Methods
abstract class Shape {
abstract double area();
}
class Square extends Shape {
double side = 4;
double area() { // must implement, or Square would need to be abstract too
return side * side;
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Square().area());
}
}
Login to try C/C++/Java/PHP code in the editor
Combining Abstract and Regular Methods
Unlike a pure interface, an abstract class can freely mix abstract methods (no body, must be overridden) with fully implemented concrete methods (with a body, inherited as-is), giving you flexibility to share real working code alongside enforced contracts.
Example: Combining Abstract and Regular Methods
abstract class Shape {
abstract double area();
void describe() { // concrete method with a body
System.out.println("Area is " + area());
}
}
class Square extends Shape {
double side = 4;
double area() { return side * side; }
}
public class Main {
public static void main(String[] args) {
new Square().describe();
}
}
Login to try C/C++/Java/PHP code in the editor
Abstract Classes with Constructors
Abstract classes can define constructors, which run automatically whenever a concrete subclass object is created (as part of the normal parent-constructor-runs-first sequence), even though the abstract class itself can never be instantiated on its own.
Example: Abstract Classes with Constructors
abstract class Shape {
String name;
Shape(String name) {
this.name = name; // runs when a concrete subclass is created
}
}
class Square extends Shape {
Square() {
super("Square");
}
}
public class Main {
public static void main(String[] args) {
System.out.println(new Square().name);
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: