π Introduction
In the previous article, we learned about method overriding and how Java decides method execution at runtime.
Now we move to one of the most important and confusing concepts in Java OOP:
π Parent Reference and Child Object
You must have seen code like:
Animal a = new Dog();
At first glance, this looks confusing:
- Why is the reference type
Animal? - Why are we creating a
Dogobject? - Which methods will be called?
These questions confuse almost every beginner.
But once you understand this concept, you unlock:
β Runtime polymorphism
β Flexible code design
β Real-world OOP understanding
β Interview-level clarity
π§ What Does A obj = new B() Mean?
General form:
Parent obj = new Child();
Example:
Animal a = new Dog();
π§© Breaking It Down
π Animal β reference type
π Dog β object type
So:
- Reference decides what can be accessed
- Object decides what actually runs
π‘ Simple Rule (Very Important)
π Reference type β decides access
π Object type β decides execution
π» Basic Example
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.eat();
// a.bark(); β ERROR
}
}
π Line-by-Line Explanation
Animal a = new Dog();
- Reference type = Animal
- Object type = Dog
a.eat();
β Allowed because eat() exists in Animal
a.bark();
β Compile-time error
π Because reference type Animal does NOT have bark()
π§Ύ Output
Animal eats
π Now Add Method Overriding
class Animal {
void sound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Animal a = new Dog();
a.sound();
}
}
π§ Output
Dog barks
π₯ Key Insight
Even though:
Animal a
π The method from Dog class is executed
Because:
π Java uses runtime polymorphism
βοΈ How Java Decides This
Java uses:
π Dynamic Method Dispatch
Meaning:
- Decision is made at runtime
- Based on object type (Dog)
π§ Memory Concept (Very Important)
When you write:
Animal a = new Dog();
Memory looks like:
Reference (Animal) β Object (Dog)
Object contains:
Animal part + Dog part
β οΈ Important Rules
β Rule 1: Upcasting (Implicit)
Animal a = new Dog();
β Allowed automatically
β Called Upcasting
β Rule 2: Downcasting (Needs Explicit Cast)
Dog d = (Dog) a;
β Allowed only with casting
β Rule 3: Invalid Assignment
Dog d = new Animal();
β Compile-time error
π Parent cannot become child
𧨠Tricky Interview Example
class A {
void show() {
System.out.println("A");
}
}
class B extends A {
void show() {
System.out.println("B");
}
void display() {
System.out.println("Display");
}
}
public class Main {
public static void main(String[] args) {
A obj = new B();
obj.show();
// obj.display(); β ERROR
}
}
π§ Output
B
β οΈ Why Error?
obj.display();
β Not allowed
π Because reference type is A
π Access vs Execution (Golden Rule)
| Concept | Decided By |
|---|---|
| Access methods | Reference type |
| Execute methods | Object type |
β οΈ Common Mistakes & Confusions
β Mistake 1: Thinking child methods are accessible
a.bark();
β Not allowed
β Mistake 2: Confusing reference with object
Students think:
βDog object means everything accessibleβ
β Wrong
β Mistake 3: Thinking compile-time decides method
β Wrong
π Runtime decides
β Mistake 4: Wrong Casting
Dog d = (Dog) new Animal();
β Runtime error (ClassCastException)
π― When Do We Use This in Real Life?
This concept is used in:
β Collections
β Frameworks
β APIs
β Loose coupling
β Dependency injection
Example:
List list = new ArrayList();
π‘ Real-Life Analogy
Think:
Animal a = new Dog();
π You say: βThis is an Animalβ
π But actual object is Dog
So behavior = Dog
Access = Animal
π§ Key Points to Remember
β Parent ref = Child object is called Upcasting
β Reference controls access
β Object controls execution
β Used for runtime polymorphism
β Child-specific methods need casting
π Conclusion
The concept of parent reference and child object is one of the most powerful ideas in Java.
It allows programs to be:
- Flexible
- Scalable
- Reusable
It is the backbone of:
π Runtime Polymorphism
In this article, we understood:
- What
A obj = new B()means - How Java handles method calls
- Difference between access and execution
- Upcasting and downcasting
- Common mistakes and interview traps
Mastering this concept will significantly improve your understanding of object-oriented programming in Java.