0
0
Javaprogramming~10 mins

Parent and child classes in Java - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parent and child classes
Define Parent Class
Define Child Class extends Parent
Create Child Object
Child inherits Parent's properties
Child can use or override Parent methods
Use Child object to call methods
End
Shows how a child class inherits from a parent class, can use or change its methods, and how objects of the child class behave.
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 Main {
  public static void main(String[] args) {
    Child c = new Child();
    c.greet();
  }
}
Defines a parent and child class where child overrides a method; creates child object and calls greet.
Execution Table
StepActionEvaluationResult
1Define Parent class with greet()No outputParent class ready
2Define Child class extends Parent with greet()No outputChild class ready, overrides greet()
3Create Child object cc is new Child()Object c created
4Call c.greet()Calls Child's greet()Prints: Hello from Child
5End of programNo more instructionsProgram stops
💡 Program ends after calling greet() on Child object
Variable Tracker
VariableStartAfter Step 3Final
cundefinedChild object createdChild object exists
Key Moments - 2 Insights
Why does c.greet() print 'Hello from Child' and not 'Hello from Parent'?
Because the Child class overrides the greet() method. Execution_table step 4 shows c.greet() calls Child's greet(), so Child's version runs.
Does the Child class have access to Parent's greet() method?
Yes, but since Child overrides greet(), its own version is used. If Child did not override greet(), Parent's greet() would run.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is printed when c.greet() is called at step 4?
A"Hello from Child"
B"Hello from Parent"
CNothing is printed
DCompilation error
💡 Hint
See execution_table row 4 where c.greet() calls Child's greet() and prints the message.
At which step is the Child object created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check execution_table row 3 where the Child object c is created.
If the Child class did not override greet(), what would c.greet() print?
A"Hello from Child"
B"Hello from Parent"
CCompilation error
DNo output
💡 Hint
Since Child inherits Parent, without override, Parent's greet() runs (see concept explanation).
Concept Snapshot
Parent and child classes in Java:
- Use 'class Child extends Parent' to inherit.
- Child inherits Parent's methods and fields.
- Child can override Parent methods to change behavior.
- Create objects of Child to use inherited or overridden methods.
- Method calls on Child objects use Child's version if overridden.
Full Transcript
This example shows how a child class inherits from a parent class in Java. First, the Parent class is defined with a greet() method that prints a message. Then, the Child class extends Parent and overrides the greet() method with its own message. When we create an object c of type Child and call c.greet(), the program runs the Child's greet() method, printing 'Hello from Child'. This demonstrates inheritance and method overriding. The execution table traces each step: defining classes, creating the object, calling the method, and ending the program. The variable tracker shows the Child object c being created and existing. Key moments clarify why the Child's greet() runs and that Child inherits Parent's methods. The quiz tests understanding of method overriding, object creation, and inheritance behavior. This is a basic but important concept in object-oriented programming.