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.