In Java, when parent and child classes are assigned to each other, the behavior of which method is called depends on the type of reference and the object it points to. This behavior is governed by polymorphism and method overriding rules. Below are the different scenarios with examples and explanations:
Scenario 1: Parent reference, Parent object
- Method Called: Methods of the parent class are called.
- No polymorphism is involved here.
class Parent {
void display() {
System.out.println("Parent's display method");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Parent();
p.display(); // Calls Parent's display method
}
}
Output:
Parent's display method
Scenario 2: Parent reference, Child object
- Method Called: The child class’s overridden method is called (runtime polymorphism).
- This is because the actual object type determines the method to invoke.
class Parent {
void display() {
System.out.println("Parent's display method");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child's display method");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
p.display(); // Calls Child's display method
}
}
Output:
Child's display method
Scenario 3: Child reference, Child object
- Method Called: The child class’s method is called.
- This is straightforward as both the reference and object belong to the child class.
class Parent {
void display() {
System.out.println("Parent's display method");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child's display method");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.display(); // Calls Child's display method
}
}
Output:
Child's display method
Scenario 4: Child reference, Parent object (NOT ALLOWED)
- This will result in a compile-time error.
- A child reference cannot point to a parent object because the child may have methods or fields not present in the parent.
class Parent {
void display() {
System.out.println("Parent's display method");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child's display method");
}
}
public class Main {
public static void main(String[] args) {
// Compile-time error
// Child c = new Parent();
}
}
Scenario 5: Explicit casting from Parent to Child
- Method Called: The child’s overridden method is called after typecasting.
- Condition: The parent reference must point to an object of the child type; otherwise, a
ClassCastExceptionwill occur.
class Parent {
void display() {
System.out.println("Parent's display method");
}
}
class Child extends Parent {
@Override
void display() {
System.out.println("Child's display method");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child(); // Parent reference pointing to Child object
Child c = (Child) p; // Explicit cast
c.display(); // Calls Child's display method
}
}
Output:
Child's display method
Scenario 6: Parent reference calling child-specific method (NOT ALLOWED)
- The parent reference cannot access methods or fields specific to the child class unless explicitly cast.
class Parent {
void display() {
System.out.println("Parent's display method");
}
}
class Child extends Parent {
void childSpecificMethod() {
System.out.println("Child-specific method");
}
}
public class Main {
public static void main(String[] args) {
Parent p = new Child();
// p.childSpecificMethod(); // Compile-time error: method not found
Child c = (Child) p; // Explicit cast
c.childSpecificMethod(); // Calls child-specific method
}
}
Output:
Child-specific method
Summary Table
| Reference Type | Object Type | Method Called |
|---|---|---|
| Parent | Parent | Parent’s method |
| Parent | Child | Child’s overridden method (runtime polymorphism) |
| Child | Child | Child’s method |
| Child | Parent | Not allowed (compile-time error) |
| Parent (cast to Child) | Child | Child’s method (after explicit cast) |
Key Points
- The object type determines the method to call (polymorphism).
- A parent reference cannot call child-specific methods without typecasting.
- A child reference cannot point to a parent object.