0
0
Javaprogramming~10 mins

Method overriding in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Method overriding
Parent class defines method
Child class defines method with same signature
Create child class object
Call method on child object
Child's method runs, overrides parent
Output from child method
Method overriding happens when a child class provides its own version of a method defined in its parent class. When called on a child object, the child's method runs instead of the parent's.
Execution Sample
Java
class Parent {
  void greet() {
    System.out.println("Hello from Parent");
  }
}
class Child extends Parent {
  void greet() {
    System.out.println("Hello from Child");
  }
}
public class Test {
  public static void main(String[] args) {
    Parent obj = new Child();
    obj.greet();
  }
}
This code shows a parent and child class both with a greet method. The child overrides the parent's greet. When calling greet on a child object, the child's greet runs.
Execution Table
StepActionObject TypeMethod CalledOutput
1Create Parent reference to Child objectChildN/AN/A
2Call greet() on objChildChild.greet()Hello from Child
3End of main methodN/AN/AN/A
💡 Program ends after main method finishes
Variable Tracker
VariableStartAfter Step 1After Step 2Final
objnullChild object referenceChild object referenceChild object reference
Key Moments - 2 Insights
Why does calling greet() on a Parent type variable run the Child's greet method?
Because the actual object is of Child type (see execution_table step 1 and 2), Java uses the object's real type to decide which method to run, not the variable's declared type.
What happens if the Child class does not override greet()?
Then calling greet() on the Child object runs the Parent's greet method, because no overriding method exists in Child (not shown in table but implied).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, which method is called when obj.greet() runs?
AParent.greet()
BChild.greet()
CBoth Parent.greet() and Child.greet()
DNo method is called
💡 Hint
Check the 'Method Called' column in execution_table step 2
At which step is the Child object created and assigned to obj?
AStep 3
BStep 2
CStep 1
DNo object is created
💡 Hint
Look at the 'Action' and 'Object Type' columns in execution_table step 1
If obj was declared as Parent and assigned a Parent object, which greet() method would run?
AParent.greet()
BChild.greet()
CBoth methods
DNo method
💡 Hint
Refer to key_moments explanation about object type deciding method call
Concept Snapshot
Method overriding lets a child class provide its own version of a method from the parent class.
When calling the method on a child object, the child's method runs, even if the variable type is the parent.
The method signature must match exactly.
This enables polymorphism and dynamic method dispatch in Java.
Full Transcript
This example shows method overriding in Java. The Parent class has a greet method. The Child class extends Parent and overrides greet with its own version. In main, a Parent type variable obj holds a Child object. When obj.greet() is called, Java runs Child's greet method because the actual object is Child. The execution table traces creating the Child object, calling greet, and the output. The variable tracker shows obj pointing to the Child object throughout. Key moments clarify why the Child method runs despite the Parent type variable. The quiz tests understanding of method calls and object types. The snapshot summarizes the key points about method overriding and dynamic dispatch.