Abstract classes, final and dynamic binding
Notes on Java abstract classes, final (variables/methods/classes) and dynamic binding (runtime method dispatch).
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Abstract classes, the final modifier and dynamic binding are building blocks of Java's object-oriented design. Engineers use abstract classes to provide common behaviour while forcing subclasses to supply specifics, use final to make APIs safe and immutable, and rely on dynamic binding to let the program choose which overridden method to run at runtime (essential for polymorphism).
Key ideas
Abstract method: a method declared with the
abstractkeyword and no body:abstract returnType name(params);. Subclasses must implement (override) abstract methods unless the subclass is also abstract. Exact statement: "A concrete (non-abstract) subclass of an abstract class must provide a concrete implementation for every inherited abstract method, or it will be a compile error." Also, an abstract method cannot be private, static, or final.final variable: a variable declared with
final. For primitives, the value cannot change after assignment. For reference types, the reference cannot be re-assigned after assignment, but the object's internal state may change (unless that object is itself immutable). Special case: blank final is a final field that is not initialized at declaration time but must be assigned exactly once before the constructor completes. For static blank final fields, the assignment must occur in a static initializer.final method: a method declared with
final. It cannot be overridden in subclasses. Exact rule:finalprevents overriding but not overloading. If a method isfinal, subclasses can still call it or use it, but cannot supply a new implementation.final class: a class declared with
final. It cannot be subclassed (no class can extend a final class). Exact rule: methods inside a final class may be final or non-final, but subclassing is prohibited.You cannot declare a class both abstract and final. Exact rule: Java syntax forbids a class being both
abstractandfinalbecauseabstractrequires subclassing to implement missing pieces whereasfinalforbids subclassing.Method overriding: a subclass supplies a method with the same signature and return type (or covariant return) as a superclass method. Overriding enables polymorphism. Use
@Overrideto help the compiler check you meant to override.Dynamic binding (also called runtime method dispatch): when you call an instance method through a reference whose compile-time type is a superclass or interface, the JVM chooses the implementation based on the object's actual runtime class. Exact statement: for non-static, non-private, non-final instance methods, Java selects the target method at runtime according to the actual object's class. This is the basis of polymorphism in Java.
Static binding (compile-time binding): applies to
staticmethods,privatemethods, andfinalmethods — the method to call is determined at compile time by the reference type. Static methods are hidden, not overridden; calling a static method through a reference uses the reference's compile-time type, not the object's runtime type.Assumptions and rules often tested in exams:
- Overriding requires same name and parameter list; return type must be same or covariant (a subclass type when returning a reference).
- When overriding, a method in the subclass cannot declare new or broader checked exceptions than the superclass method; it may declare fewer or more specific checked exceptions. Unchecked exceptions are not restricted.
- Access level of overriding method cannot be more restrictive than the overridden method (e.g., cannot make a
publicsuperclass methodprotectedin subclass). finalmethods cannot be overridden; trying to override gives a compile-time error.- Constructors are not inherited and cannot be abstract or final.
What to draw when asked to explain dynamic binding: draw two boxes labelled
Reference type: SuperandObject type: Subclass. Draw arrow from reference to object, and show method callref.someMethod()with note: "method implemented in Subclass will run due to dynamic binding (if overridden and not final)."
Formulas
(Syntax lines to remember)
abstract class ClassName { ... } — declares an abstract class.
abstract returnType methodName(parameterList); — declares an abstract method (no body).
final type name = value; — final variable initialized at declaration.
final class ClassName { ... } — class cannot be extended.
class Sub extends Super { @Override void m() { ... } } — overriding example; use @Override.
static returnType methodName(...) { ... } — static methods are bound at compile time.
Dynamic binding rule (text): For an expression S ref = new T(); ref.m(); where S is a superclass/interface of T, if m is an instance method that is overridden in T, then the implementation in T runs at runtime.
Worked examples
Example 1 — Abstract class and implementation
Program:
abstract class Shape {
abstract double area();
void display() {
System.out.println("Area = " + area());
}
}
class Circle extends Shape {
private double r;
Circle(double r) { this.r = r; }
@Override
double area() { return Math.PI * r * r; }
}
public class Main1 {
public static void main(String[] args) {
Shape s = new Circle(2.0);
s.display();
}
}
Sample input: (no input)
Exact output:
Area = 12.566370614359172
Example 2 — final method and final variable
Program:
class Parent {
final void show() {
System.out.println("Parent show");
}
}
class Child extends Parent {
// Cannot override show(); attempting to do so causes a compile-time error.
void doSomething() {
System.out.println("Child does something");
}
}
public class Main2 {
public static void main(String[] args) {
Parent p = new Child();
p.show();
// p.doSomething(); // compile error: method not in Parent
}
}
Sample input: (no input)
Exact output:
Parent show
Example 3 — Dynamic binding vs static binding
Program:
class Animal {
void sound() { System.out.println("Some sound"); }
static void staticMethod() { System.out.println("Animal static"); }
}
class Dog extends Animal {
@Override
void sound() { System.out.println("Bark"); }
static void staticMethod() { System.out.println("Dog static"); }
}
public class Main3 {
public static void main(String[] args) {
Animal a = new Dog();
a.sound(); // dynamic binding -> Dog.sound()
Animal.staticMethod(); // static binding -> Animal.staticMethod()
Dog.staticMethod(); // Dog.staticMethod()
}
}
Sample input: (no input)
Exact output:
Bark Animal static Dog static
Check: these outputs assume Math.PI from standard Java.
Common mistakes
- Attempting to override a
finalmethod or extend afinalclass. (Compile-time error.) - Confusing overriding with overloading: overloading is same method name, different parameters (compile-time choice), overriding is same signature (runtime choice).
- Expecting static methods to use dynamic binding. Static methods are resolved at compile time by reference type.
- Expecting fields to be overridden/polymorphic. Field access is resolved by the declared reference type (fields are hidden, not overridden).
- Forgetting to implement all inherited abstract methods in a concrete subclass.
- Using
@Overrideincorrectly (compiler will catch mistakes if signature doesn't match).
For your exam
Possible question types and what full-marks answers include:
- Definitions: define abstract class, abstract method, final variable/method/class, dynamic binding. Include exact syntax and one-line examples.
- Short note: "Why use abstract classes vs interfaces?" — list differences (stay inside this topic: you may refer to Interfaces in Java by name but not re-teach them). Mention abstract classes can have state and constructors; interfaces (modern Java) differ; exams expect advantages and examples.
- Programs: write a program showing an abstract class and subclass or demonstrating dynamic binding. Provide working code, compile and run output. Explain each line briefly.
- True/False or MCQ: statements about overriding, final, dynamic binding — justify the answer with a short reason.
- Diagram question: draw class hierarchy and show which method will execute for calls through superclass reference (explain dynamic binding).
- Marks: for programs, include full source code, sample output, and short explanation of how dynamic binding produced the output.
What full-marks answers must include:
- Precise use of Java keywords in code (
abstract,final,@Override). - Explanation of why code compiles or fails (state the compile error message in words when relevant).
- Correct sample output matching the program.
Quick check
- Can you create an object of an abstract class? Explain.
- Does
finalon a reference variable prevent changes to the object's internal fields? Yes or no and why. - If
A a = new B();and both classes define instance methodm()with B overriding m, which class's m runs ona.m()? Why?
Answers: 1) No — abstract classes cannot be instantiated; you must instantiate a concrete subclass. 2) No — final prevents reassigning the reference, but does not prevent mutation of the object's internal state. 3) B's m runs — because Java uses dynamic binding (runtime method dispatch) for non-static instance methods, the method in the actual object's class (B) is chosen.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?