-3

I’m trying to understand upcasting with Java. I just created two classes Mom and Girl, with each having their own constructor (constructors have no parameters).

If I understand, Mom mom = new Girl() causes the instance created by new Girl to be cast to a Mom type, and that is confirmed by the fact that the Mom constructor is being called. So mom variable is pointing to a Mom instance, right? So, why, when I overwrite a Mom method test() in the Girl class, it causes the Girl method test() to be called instead of the Mom method? It's just so confusing: on one side Mom constructor is being called on the other side, the Girl method is called, like if mom variable has access to both classes.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Idash
  • 55
  • 8
  • 3
    Please don't describe your code. Instead [edit] your question to include a [example]. – Federico klez Culloca Jan 20 '23 at 08:10
  • 2
    Agreed - but fundamentally "So mom variable is pointing to a Mom instance right" is wrong, or at least incomplete. You still end up with an instance of Girl. If you call `mom.getClass()` it will return `Girl`. A `Girl` instance is *also* an instance of `Mom` (in terms of inheritance) but the specific concrete class is `Girl`, because that's what you created with the expression `new Girl()`. The type of the variable doesn't change the type of the object. – Jon Skeet Jan 20 '23 at 08:18
  • @JonSkeet thank you for your help, very helpful, but one more question, if the object is still a Girl object, why is Mom constructor being called ? – Idash Jan 20 '23 at 08:25
  • Please don't add follow-up questions in comment threads. I would strongly recommend that you find a good book or web site explaining inheritance thoroughly - otherwise every answer is going to lead to another question that's already explained in documentation/books. – Jon Skeet Jan 20 '23 at 08:26
  • Oracle has some good onlie tutorials: https://docs.oracle.com/javase/tutorial/ (but unfortunately not updated since Java SE 8) – Puce Jan 20 '23 at 08:27
  • 3
    Please note that with this class hierarchy every instance of Girl is also an instance Mom. I don't know your exact assignment but "every girl is a mom" sounds wrong. Maybe you have your class hierarchy/ association wrong? – Puce Jan 20 '23 at 08:35

1 Answers1

1

The reference variable Mom mom can reference any instance of Mom or instances of subclasses of Mom.

In your case the instance is still a Girl-instance, which is referenced by a Mom reference variable. You have access to any members of Mom (declaration type), but not to the members of Girl. But since the instance is in fact a Girl instance, the implementation of the Girl class will be executed.

This mechanism is called "polymorphism".

Puce
  • 37,247
  • 13
  • 80
  • 152
  • thank you for your response but same question as below, if the object is still a Girl object, why is Mom constructor being called ? – Idash Jan 20 '23 at 08:27
  • @Idash since a Girl instance "is-a" Mom instance as well, it has to pass the Mom initialization as well. – Puce Jan 20 '23 at 08:30
  • Both constructors will be called. – Dean Johnson Jan 20 '23 at 08:36
  • @DeanJohnson I got it thank you, one last question please, in that case why "Girl girl = mom" needs to be written "Girl girl = (Girl) mom" since mom is pointing to a Girl instance ? – Idash Jan 20 '23 at 08:52
  • For that, see https://stackoverflow.com/questions/380813/downcasting-in-java. Basically, the compiler can't guarantee that line is correct and so it needs your help. If you are wrong, you will get an exception in Java. – Dean Johnson Jan 20 '23 at 08:56
  • @Idash: As I wrote before: "Please don't add follow-up questions in comment threads." That isn't just for *me* - it's *in general on Stack Overflow*. Stack Overflow isn't designed to be a "just one more question" sort of help-desk. – Jon Skeet Jan 20 '23 at 09:17