Bird
Raised Fist0
Javaprogramming~10 mins

Abstract methods in Java - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Abstract methods
Define abstract class
Declare abstract method (no body)
Subclass extends abstract class
Subclass must override abstract method
Create subclass object
Call overridden method
Execute subclass method body
Abstract methods are declared without a body in an abstract class. Subclasses must provide the method's body.
Execution Sample
Java
abstract class Animal {
  abstract void sound();
}

class Dog extends Animal {
  void sound() { System.out.println("Bark"); }
}

public class Main {
  public static void main(String[] args) {
    Dog d = new Dog();
    d.sound();
  }
}
This code defines an abstract method sound() in Animal, implements it in Dog, then calls it.
Execution Table
StepActionEvaluationResult
1Define abstract class Animal with abstract method sound()No method bodyAnimal cannot be instantiated
2Define subclass Dog extending AnimalDog must override sound()Dog provides sound() method with body
3Create Dog object dDog is concrete, so instantiation allowedObject d created
4Call d.sound()Calls Dog's overridden sound()Prints 'Bark'
5End of executionNo more statementsProgram ends
💡 Program ends after calling overridden method in subclass
Variable Tracker
VariableStartAfter 1After 2After 3Final
d (Dog object)nullnullnullDog instance createdDog instance exists
Key Moments - 3 Insights
Why can't we create an object of the abstract class Animal?
Because Animal has an abstract method without a body (see step 1 in execution_table), Java forbids creating its objects.
What happens if Dog does not override the abstract method sound()?
The code will not compile because Dog must provide a body for all abstract methods inherited (step 2).
When we call d.sound(), which method runs?
The overridden method in Dog runs, printing 'Bark' (step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result of step 4?
ACompilation error
BPrints nothing
CPrints 'Bark'
DRuntime error
💡 Hint
Check step 4 in execution_table where d.sound() is called
At which step is the Dog object created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at variable_tracker and execution_table step 3 for object creation
If we remove the method body from Dog's sound(), what happens?
ACompilation error because Dog must override abstract method
BRuntime error when calling sound()
CProgram runs and prints 'Bark'
DAnimal can be instantiated
💡 Hint
Refer to key_moments about overriding abstract methods
Concept Snapshot
abstract class ClassName {
  abstract returnType methodName(); // no body
}

Subclass must override abstract methods.
Cannot instantiate abstract class.
Calling overridden method runs subclass code.
Full Transcript
Abstract methods are declared in abstract classes without a body. You cannot create objects of abstract classes. Subclasses must provide the method body by overriding the abstract method. When you create an object of the subclass and call the method, the subclass's version runs. This ensures a common method signature with different implementations.

Practice

(1/5)
1. Which statement about abstract methods in Java is correct?
easy
A. Abstract methods have no body and must be implemented by subclasses.
B. Abstract methods can have a body and be called directly.
C. Abstract methods are only used in interfaces, not classes.
D. Abstract methods can be private and final.

Solution

  1. Step 1: Understand abstract method definition

    Abstract methods declare a method signature without a body, forcing subclasses to provide implementation.
  2. Step 2: Check each option against the definition

    Abstract methods have no body and must be implemented by subclasses. correctly states abstract methods have no body and must be implemented by subclasses. Options A, B, and D are incorrect because abstract methods cannot have bodies, are used in abstract classes (not only interfaces), and cannot be private or final.
  3. Final Answer:

    Abstract methods have no body and must be implemented by subclasses. -> Option A
  4. Quick Check:

    Abstract method = no body, must implement [OK]
Hint: Abstract methods have no body and require subclass implementation [OK]
Common Mistakes:
  • Thinking abstract methods can have a body
  • Confusing abstract methods with interface methods
  • Believing abstract methods can be private or final
2. Which of the following is the correct syntax to declare an abstract method in Java?
easy
A. public void abstract display();
B. void abstract display();
C. abstract public void display() {}
D. public abstract void display();

Solution

  1. Step 1: Recall abstract method syntax

    Abstract methods must be declared with the keyword abstract, have no body, and specify the return type and method name.
  2. Step 2: Evaluate each option

    public abstract void display(); correctly declares an abstract method with no body. public void abstract display(); incorrectly places abstract after void. abstract public void display() {} incorrectly provides a method body ({}). void abstract display(); misses the access modifier and places abstract incorrectly.
  3. Final Answer:

    public abstract void display(); -> Option D
  4. Quick Check:

    abstract method = abstract + no body [OK]
Hint: Abstract method syntax: 'abstract' before return type, no body [OK]
Common Mistakes:
  • Adding method body {} to abstract methods
  • Placing 'abstract' keyword incorrectly
  • Omitting access modifiers (though optional)
3. What will be the output of the following Java code?
abstract class Animal {
    abstract void sound();
}

class Dog extends Animal {
    void sound() {
        System.out.println("Bark");
    }
}

public class Test {
    public static void main(String[] args) {
        Animal a = new Dog();
        a.sound();
    }
}
medium
A. Animal sound
B. Bark
C. Compilation error due to abstract method
D. Runtime error

Solution

  1. Step 1: Understand class hierarchy and method implementation

    Class Animal is abstract with an abstract method sound(). Class Dog extends Animal and provides implementation for sound() that prints "Bark".
  2. Step 2: Analyze main method execution

    In main, an Animal reference points to a Dog object. Calling sound() invokes Dog's implementation, printing "Bark".
  3. Final Answer:

    Bark -> Option B
  4. Quick Check:

    Abstract method implemented in subclass = prints subclass output [OK]
Hint: Abstract method calls subclass implementation at runtime [OK]
Common Mistakes:
  • Expecting abstract method to run without implementation
  • Confusing abstract class instantiation
  • Thinking abstract method prints default text
4. Identify the error in the following Java code:
abstract class Shape {
    abstract void draw();
}

class Circle extends Shape {
    void draw() {
        System.out.println("Drawing Circle");
    }
}

class Square extends Shape {
}

public class Test {
    public static void main(String[] args) {
        Shape s = new Square();
        s.draw();
    }
}
medium
A. Method draw() in Circle should be abstract
B. Cannot create object of abstract class Shape
C. Class Square must implement the abstract method draw()
D. No error, code runs fine

Solution

  1. Step 1: Check abstract method implementation in subclasses

    Class Shape has abstract method draw(). Circle correctly implements it. Square does not implement draw().
  2. Step 2: Understand consequences of missing implementation

    Since Square does not implement the abstract method, it must be declared abstract or implement the method. Otherwise, compilation error occurs.
  3. Final Answer:

    Class Square must implement the abstract method draw() -> Option C
  4. Quick Check:

    Subclass must implement all abstract methods [OK]
Hint: All abstract methods must be implemented or subclass declared abstract [OK]
Common Mistakes:
  • Forgetting to implement abstract methods in subclasses
  • Trying to instantiate abstract classes directly
  • Misunderstanding abstract method requirements
5. You want to design a Java program where different vehicle types must provide their own startEngine() behavior. Which approach using abstract methods is best to ensure this?
hard
A. Create an abstract class Vehicle with an abstract method startEngine(), then subclass it for each vehicle type implementing startEngine().
B. Create a concrete class Vehicle with a startEngine() method that prints a generic message, and override it in subclasses.
C. Create an interface Vehicle with a default startEngine() method, and override it in subclasses if needed.
D. Create a class Vehicle with a private startEngine() method and call it from subclasses.

Solution

  1. Step 1: Understand requirement for mandatory implementation

    The program requires each vehicle type to provide its own startEngine() behavior, so subclasses must be forced to implement this method.
  2. Step 2: Evaluate options for enforcing implementation

    Create an abstract class Vehicle with an abstract method startEngine(), then subclass it for each vehicle type implementing startEngine(). uses an abstract class with an abstract method, forcing subclasses to implement startEngine(). Create a concrete class Vehicle with a startEngine() method that prints a generic message, and override it in subclasses. provides a generic method that can be overridden but does not force implementation. Create an interface Vehicle with a default startEngine() method, and override it in subclasses if needed. uses an interface with a default method, which subclasses may skip overriding. Create a class Vehicle with a private startEngine() method and call it from subclasses. uses a private method, which is not accessible to subclasses.
  3. Final Answer:

    Create an abstract class Vehicle with an abstract method startEngine(), then subclass it for each vehicle type implementing startEngine(). -> Option A
  4. Quick Check:

    Abstract method forces subclass implementation [OK]
Hint: Use abstract method in abstract class to force subclass implementation [OK]
Common Mistakes:
  • Using concrete methods without forcing override
  • Using private methods inaccessible to subclasses
  • Relying on default interface methods without enforcement