-3

Imagine there's two Java classes, Dog & Poodle. The Poodle class extends the Dog class.

When creating an instance of these classes, this is the common syntax:

Dog myDog = new Dog();
Poodle myPoodle = new Poodle();

I don't understand what's happening though when you create an object of type parent class and then call the child class constructor, like so:

Dog myDog = new Poodle();

If myDog is a Dog class, why would we call the constructor of one of its child classes, and what does that even mean? Why not just create an object of type child class (Poodle)?

To put another way, I don't know what this sentence means: "I'm going to create an object of type Dog but I'm going to call the constructor of one of its child classes, even though this object is not of the child class type".

I can't make any logical sense of what this means. Please help me understand what is going on here.

EDIT - the linked duplicate question indeed appears to be asking the same question. Frustratingly, the two most upvoted answers on that page don't provide an explicit example of a meaningful use of the code segment that the questioner specifically asked about:

Parent parent = new Child();

I understood and followed all code segments provided by the two most upvoted answers on the linked duplicate question. I even understood where the polymorphism took place. But neither of the answers used the line of code specifically called out in the question. I would really, really appreciate if someone could please show me an example of where

Parent parent = new Child();

is used usefully and meaningfully in a broader segment of code. Please. I would really, really appreciate it.

Callum
  • 435
  • 1
  • 4
  • 14
  • 1
    Possible duplicate of https://stackoverflow.com/questions/12159601/why-do-we-assign-a-parent-reference-to-the-child-object-in-java – raviraja Aug 14 '19 at 08:04
  • 2
    You've already asked a similar question like 30 minutes ago. I suggest you actually read the other questions that are linked as duplicates or - if you have difficulties understanding it - grab a tutorial on inheritance. To answer your question: you create an object when you call a class' constructor, so `new Poodle()` will create a `Poodle` instance. If `Poodle extends Dog` a variable of type `Dog` can refer to a `Poodle` object but that variable itself _is not an object_ but a reference. – Thomas Aug 14 '19 at 08:09
  • StackOverflow would not let me delete, or mark as duplicate, my other question. – Callum Aug 14 '19 at 08:16
  • @raviraja I'm no closer to grasping the answer to my Q having gone through every answer on the linked duplicate page. I have edited my question in the hopes that someone can come along to please assist with my understanding. Thanks. – Callum Aug 14 '19 at 09:37
  • @khelwood I've gone through the duplicate page and couldn't find an answer. I have added an edit to my Original Q. – Callum Aug 14 '19 at 09:51

1 Answers1

0

The concept is called Dynamic method dispatch, by using this run time polymorphism(Overriding) is achieved in java.

Let's extend the example you wrote as below

class Dog{

public void bark(){
// parent class implementation
System.out.println("I bark");
}

}

Here we have defined a Dog class which has a method called bark and provides it's implementation as well.

Let's say Dog is having two subclasses as Poodle and Chihuahua as below.

class Poodle extends Dog{

@Override
public void bark(){
//Poodle implementation
System.out.println("I bark like Poodle");
}
}

class Chihuahua extends Dog{

@Override
public void bark(){
//Chihuahua implementation
System.out.println("I bark like Chihuahua");
}
}

Poodle and Chihuahua classes have their implementation of the bark method.

And you created a child object and assigned a parent reference to it and call bark method as below.

Dog dog1 = new Poodle();
dog1.bark(); //prints **I bark like Poodle**

At the run time JVM understands that you have a Poodle Object and checks if Poodle overrides the bark method and then calls it.

This pattern can be observed in Collections when you do something like below.

List<String> list = new ArrayList<>();
list.add("test string"); //this gets added according to ArrayList implementation.
raviraja
  • 676
  • 10
  • 27
  • Thanks ravijara, I think I now get the concept, but I still can't conceive of a real world situation where you'd write Dog dog1 = new Poodle(); – Callum Aug 15 '19 at 08:03
  • I have added an example at the end about `List`, which is used heavily in real-world situations. – raviraja Aug 15 '19 at 08:06
  • In your example, why not declare list as an ArrayList type? What's the benefit of limiting the exposed interface to only List methods? – Callum Aug 15 '19 at 23:42