4-πŸ”„ Parent Reference & Child Object in Java – Understanding A obj = new B() Clearly

🌟 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 Dog object?
  • 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)

ConceptDecided By
Access methodsReference type
Execute methodsObject 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.


Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *