0
0
Javaprogramming~10 mins

Abstract methods in Java - Step-by-Step Execution

Choose your learning style9 modes available
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.