10.1 Abstract Classes & Abstract Methods
Key Takeaways
An abstract class cannot be instantiated directly with new, but it can define constructors that run via super() when a subclass is instantiated
An abstract class may contain zero or more abstract methods alongside concrete methods, instance fields, static members, and initializer blocks
An abstract class method has no implementation body and ends with a semicolon. It cannot be private, static, final, native, strictfp, or synchronized.
A class cannot be both abstract and final. A normal abstract class forbids direct instantiation, while final would prohibit every subclass.
A concrete subclass must have implementations for its remaining abstract contracts. It can inherit a suitable concrete implementation.
Why Abstract Classes Matter
Domain 7 (Working with Inheritance) of the Oracle Java SE 8 Programmer I (1Z0-808) exam tests your ability to distinguish when a base type can be instantiated directly, when a method contract must be deferred to subclasses, and which modifier combinations the compiler rejects. An abstract class represents an incomplete template in an inheritance hierarchy: it captures shared state and behavior while leaving specific operations unimplemented—or simply prevents callers from instantiating a general base concept directly.
Declaring an Abstract Class
To declare an abstract class, include the abstract keyword in the class header. The abstract modifier may appear either before or after the access modifier (public abstract class Account or abstract public class Account), as long as it precedes class.
An abstract class has far fewer restrictions than many exam candidates expect. It may contain:
- Zero or more
abstractmethods — anabstractclass is not required to declare anyabstractmethods at all - Concrete instance methods and
staticmethods - Instance fields (mutable or
final, with any access modifier) andstaticfields - Constructors and instance/static initializer blocks
public abstract class Account {
private final String accountId;
protected double balance;
private static int openAccounts = 0;
public Account(String accountId, double initialBalance) {
this.accountId = accountId;
this.balance = initialBalance;
openAccounts++;
System.out.println("Account constructor: " + accountId);
}
// Concrete method shared by all subclasses
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public static int getOpenAccounts() {
return openAccounts;
}
// Abstract method: no body, ends with a semicolon
public abstract double calculateMonthlyFee();
}
Note
Even though an abstract class cannot be instantiated directly, the compiler still generates a default no-argument constructor if you do not write any constructor explicitly. Subclasses always invoke a superclass constructor (explicitly with super(...) or implicitly via super()) during object creation.
Instantiation Rules and Anonymous Subclasses
Attempting to instantiate an abstract class directly with new fails at compile time:
Account a = new Account("A-100", 500.0); // Compile error: Account is abstract; cannot be instantiated
However, the 1Z0-808 exam often pairs that rule with an anonymous subclass. If you follow new Account("A-100", 500.0) with a class body { ... } that implements every unimplemented abstract method, the expression compiles cleanly:
public class AccountDemo {
public static void main(String[] args) {
Account temp = new Account("A-100", 500.0) {
@Override
public double calculateMonthlyFee() {
return balance < 1000.0 ? 12.0 : 0.0;
}
};
System.out.println("Fee: " + temp.calculateMonthlyFee());
}
}
Output:
Account constructor: A-100
Fee: 12.0
Why did new Account(...) { ... } compile? Because you did not instantiate Account itself—you defined and instantiated an unnamed concrete subclass of Account, which invoked Account(String, double) before running calculateMonthlyFee().
Abstract Method Syntax and Illegal Modifiers
An abstract method declares a signature without an implementation body. Replace the curly braces {} with a semicolon ;.
Every abstract method must follow three strict rules checked by javac:
- Enclosing normal class: It must be abstract to declare or leave an abstract method unimplemented. Enums have their own rule. An enum with an abstract method needs each constant’s class body to supply a concrete implementation.
- No method body: Adding
{}to anabstractmethod fails to compile (abstract methods cannot have a body), even if the braces are empty. - Valid modifier combinations only:
abstractcannot be paired withprivate,static,final,native,strictfp, orsynchronizedon a class method.
| Declaration | Compiles? | Compiler Reason / Rule |
|---|---|---|
public abstract class EmptyBase { } | Yes | An abstract class can have zero abstract methods |
public abstract final class Locked { } | No | A normal abstract class cannot be instantiated directly, and a final class cannot have subclasses |
private abstract void run(); | No | Subclasses cannot see or override a private method |
public static abstract void run(); | No | static methods are hidden, never overridden polymorphically |
public final abstract void run(); | No | final forbids overriding, contradicting abstract |
public abstract void run() { } | No | abstract methods cannot declare a {} body |
class Plain { abstract void run(); } | No | Concrete class Plain cannot declare an abstract method |
Warning
Watch closely for abstract final class and abstract static void on exam code-reading questions. Both produce an immediate compile-time error: illegal combination of modifiers.
Concrete vs. Abstract Subclasses in Multi-Level Hierarchies
When a class extends an abstract class, it becomes either abstract or concrete:
- An abstract subclass may implement all, some, or none of its parent's
abstractmethods. It can also introduce newabstractmethods, or even redeclare an inherited concrete method asabstract. - A concrete subclass needs a compatible concrete implementation for each remaining abstract method. That implementation can be declared in the subclass or inherited. When writing an override, check the parameter signature, return type, access, and checked exceptions.
Trace the three-level hierarchy below:
abstract class Vehicle {
protected String model;
public Vehicle(String model) {
this.model = model;
System.out.println("1. Vehicle: " + model);
}
public abstract int maxRange();
public abstract String fuelType();
}
abstract class ElectricVehicle extends Vehicle {
public ElectricVehicle(String model) {
super(model);
System.out.println("2. ElectricVehicle");
}
// Implements one inherited abstract method
@Override
public String fuelType() {
return "Electricity";
}
// Leaves maxRange() unimplemented and adds a new abstract method
public abstract int batteryKwh();
}
public class SedanEV extends ElectricVehicle {
public SedanEV(String model) {
super(model);
System.out.println("3. SedanEV");
}
@Override
public int maxRange() {
return 310;
}
@Override
public int batteryKwh() {
return 75;
}
public static void main(String[] args) {
Vehicle v = new SedanEV("Model S");
System.out.println(v.fuelType() + " | Range: " + v.maxRange());
}
}
Output:
1. Vehicle: Model S
2. ElectricVehicle
3. SedanEV
Electricity | Range: 310
Notice two exam-critical details in main: first, all three constructors execute top-down starting at Vehicle; second, calling v.batteryKwh() on the Vehicle-typed reference v would not compile because Vehicle does not declare batteryKwh(), even though the runtime object is a SedanEV.
Quick Reference: Concrete vs. Abstract Classes
| Feature | Concrete Class | Abstract Class |
|---|---|---|
Instantiated directly with new | Yes (if accessible constructor exists) | No (compile error unless anonymous subclass body provided) |
Can declare abstract methods | No | Yes (zero or more) |
| Can have constructors | Yes | Yes (executed via super(...) from subclasses) |
Can be marked final | Yes | No (abstract final is illegal) |
Can contain static and private methods | Yes | Yes (concrete only, never abstract) |
Given the code below, what is the result?
abstract class Base {
Base() { System.out.print("B"); }
void ping() { System.out.print("P"); }
}
public class Sub extends Base {
Sub() { System.out.print("S"); }
public static void main(String[] args) {
Base b = new Sub();
b.ping();
}
}
Compilation fails because Base has no abstract methods
Prints BSP
Compilation fails because Base cannot declare a constructor
Prints SP
Which method declaration inside public abstract class Report compiles without error in Java SE 8?
private abstract void export();
public static abstract String title();
public final abstract int pageCount();
protected abstract void render();
All three classes are in the same package. Consider this hierarchy: abstract class A declares abstract void m1(); and abstract void m2();; abstract class B extends A implements only m1(); and class C extends B is concrete. What must class C do to compile?
Implement m2() with public, protected, or package-private access matching or broader than A's declaration
Implement both m1() and m2() because B cannot partially implement A
Declare C as final so no further subclasses are needed
Provide a constructor that calls super.m2()
Sections you finish are checked off in the contents.