Java Share a Partial Implementation
An abstract class can combine a shared implementation with operations that concrete subclasses must provide. It cannot be instantiated directly.
Understand the idea
Use an abstract class when related objects genuinely share state or a stable template of behaviour. An abstract method has no implementation in that declaration. A concrete subtype must implement the remaining abstract operations. Keep constructor work predictable; calling an overridable method from a superclass constructor can reach a subtype before its fields are ready.
Run it locally
Use a local JDK 21 or newer. Save the complete example as Main.java in a separate lab folder, then compile and run it with the commands below. Java examples do not run in Bhylu’s browser playground. The -ea option enables assertion checks used by some lessons.
javac --release 21 -encoding UTF-8 -d out Main.java
java -ea -cp out MainA worked example
public class Main {
static abstract class Report {
abstract String content();
final String render() { return "Report: " + content(); }
}
static final class AttendanceReport extends Report {
@Override String content() { return "12 visitors"; }
}
public static void main(String[] args) {
Report report = new AttendanceReport();
System.out.println(report.render());
}
}
Expected output
Report: 12 visitorsrender supplies the shared wrapper and delegates only the content decision. final prevents a subtype from bypassing that wrapper in this small example. The object is still a concrete AttendanceReport even though the caller holds a Report reference. An interface may fit better if no shared state or implementation template is needed.
Check the official Java documentation for detailed contracts and supported options.