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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.