Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

10
  • Does Dog implement or extend Puppy? We don't know 'cause we don't see the code of these types. Commented Aug 14, 2019 at 7:32
  • 1
    Read about Polymorphism in Java. That will help you understand what is going on in there. Commented Aug 14, 2019 at 7:33
  • 1
    The line you quoted seems wrong. Dog myDog = new Puppy("tommy"); would work because Puppy is a subclass of Dog; the other way does not work because not all dogs are puppies. Commented Aug 14, 2019 at 7:34
  • @Jesper While this specific example doesn't make much sense, it's perfectly valid code if Dog extends the class Puppy (or implements it if Puppy is an interface). Commented Aug 14, 2019 at 7:38
  • 1
    And of course, the misconception is that you think that the type used on the left hand side of your declaration is in any way "related" to the constructor call on the right hand side. But it isn't. You are calling a constructor, of a specific class ... and the result of that is then addresses using the declared type. Commented Aug 14, 2019 at 8:00