Abstraction describes what an object promises to do while leaving selected implementation details to concrete types. In Java, interfaces express capabilities and abstract classes can combine shared state with unfinished behavior. Choosing between them depends on the contract you want callers to use.
Review the related foundation lesson when you need a prerequisite. The complete programs below use Java 17-compatible syntax.
Interface method visibility
Interface abstract methods are public. An implementation must not reduce that visibility. Interfaces can also declare default and static methods.
Default methods provide interface behavior that implementations may inherit or override. Static interface methods belong to that interface and are called through its name. Private interface methods can support other interface methods without publishing an additional capability. Conflicting inherited defaults need deliberate resolution; Java cannot simply guess which unrelated behavior a class intended to use.
Abstract classes
An abstract class can hold instance state, constructors and both concrete and abstract methods. It cannot be instantiated directly.
An abstract class may contain constructors, instance fields, concrete methods and abstract methods. It cannot be instantiated directly, but its constructor runs while a concrete subclass is initialized. The subclass must implement inherited abstract methods unless it is abstract too. This permits common setup without pretending that the incomplete base type already provides every required operation.
Choosing a useful abstraction
Choose an interface for a shared capability across unrelated classes. Choose an abstract base class when related implementations genuinely share state or construction rules.
Depend on the smallest useful abstraction. A report service that needs a printable description can accept a Describable interface rather than a specific database entity. An abstract class is useful when closely related implementations share setup or state, but Java's single class inheritance limits that choice. Composition can share behavior without forcing every collaborator into one inheritance tree.
Running the examples
Save name.java, compile it with javac --release 17 name.java, and run java name. Use each program's own public class name and filename.
How PricePolicyAbstraction produces its output
PricePolicy declares a price operation without implementing the pricing decision. YouthPrice implements that contract and returns five for an age below eighteen, otherwise eight. The policy reference holds a YouthPrice, so calling price(15) returns five. println combines that result with Ticket: to display Ticket: 5. No abstract class is used in this first interface example.
How AbstractReadings produces its output
Temperature calls the Reading constructor with eighteen, which initializes the inherited final value. The describe override turns that shared state into a temperature description. main can invoke describe through a Reading reference because the abstract class declares the operation, while the concrete object supplies the executable body.
How PrintableContract produces its output
print accepts any Printable implementation rather than depending on Receipt. Receipt implements the public text method and returns Paid. Passing the receipt into print copies its reference into the interface-typed parameter. The text call dispatches to the receipt's implementation, demonstrating a capability contract without shared superclass state.
Conflicting defaults require an explicit choice
If two unrelated interfaces supply the same default method signature, a class implementing both must resolve the conflict with its own implementation. It can deliberately invoke an appropriate direct superinterface default using InterfaceName.super.method(), or provide a new combined behavior. Do not assume the compiler selects whichever interface appears first in the implements clause. A concrete superclass method also affects default-method resolution, so shared behavior should be understood from the entire inheritance relationship. This is different from implementing two interfaces that merely declare the same compatible abstract operation: one public implementation can satisfy both contracts without choosing between competing default bodies.
Java Summary
- Abstraction exposes the operations a caller needs while hiding implementation details. An interface defines a contract that different classes can implement.
- Interface abstract methods are public. An implementation must not reduce that visibility. Interfaces can also declare default and static methods.
- An abstract class can hold instance state, constructors and both concrete and abstract methods. It cannot be instantiated directly.
- Choose an interface for a shared capability across unrelated classes. Choose an abstract base class when related implementations genuinely share state or construction rules.
- Continue with Java exercises, Java quizzes and the Java study roadmap.
Java Examples
Each example shows the code, the output it produces and what each part does.
Java Abstraction and Interfaces in a complete program
public class PricePolicyAbstraction {
interface PricePolicy {
int price(int age);
}
static class YouthPrice implements PricePolicy {
public int price(int age) {
return age < 18 ? 5 : 8;
}
}
public static void main(String[] args) {
PricePolicy policy = new YouthPrice();
System.out.println("Ticket: " + policy.price(15));
}
}Ticket: 5Line-by-line explanation
- Line 12Evaluates
"Ticket: " + policy.price(15)and printsTicket: 5.
An abstract class can share initialization
public class AbstractReadings {
static abstract class Reading {
final int value;
Reading(int value) {
this.value = value;
}
abstract String describe();
}
static class Temperature extends Reading {
Temperature(int value) {
super(value);
}
String describe() {
return value + " C";
}
}
public static void main(String[] args) {
Reading reading = new Temperature(18);
System.out.println(reading.describe());
}
}18 CLine-by-line explanation
- Line 19Evaluates
reading.describe()and prints18 C.
An interface describes a capability
public class PrintableContract {
interface Printable {
String text();
}
static class Receipt implements Printable {
public String text() {
return "Paid";
}
}
static void print(Printable item) {
System.out.println(item.text());
}
public static void main(String[] args) {
print(new Receipt());
}
}PaidLine-by-line explanation
- Line 11Evaluates
item.text()and printsPaid.
Java FAQ
Can an abstract class have a constructor?
Yes. Its constructor initializes the superclass portion of concrete subclass objects. You cannot construct the abstract class directly, but that restriction does not remove its need to initialize fields or validate constructor arguments.
Must every interface method be abstract?
No. Interfaces can provide default, static and appropriate private methods. Abstract capability methods remain important, but modern interfaces can also share carefully chosen behavior without becoming ordinary mutable object classes.
Can I create an object from an interface?
Not with new InterfaceName() alone, because an interface has no complete implementation. You create an object of a class that implements it, or supply an implementation on the spot with an anonymous class or, for a functional interface, a lambda. The variable can still use the interface as its type.
Can an abstract class have no abstract methods?
Yes. Marking a class abstract without any abstract methods simply prevents it from being instantiated directly, which is useful for a base class that only makes sense through its subclasses. Most abstract classes do declare at least one abstract method, though.
Is abstraction the same thing as an interface?
No. Abstraction is the design idea of exposing what something does while hiding how. Interfaces and abstract classes are the Java features that express it, but a well-named method that hides a complicated calculation is also abstraction.
Java Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Why must Receipt.text be public when implementing Printable.text?
Can an abstract class contain concrete methods?
Java Quiz
2 questions to check what you learned.
What is a useful reason to choose an interface?
- Unrelated classes can share a capability contract
- Interfaces store independent instance fields
- An interface permits direct new of the incomplete type
Which type can be instantiated in the reading example?
- Temperature
- Reading
- The abstract describe method