• Post Reply Bookmark Topic Watch Topic
  • New Topic

Small Question: In the story of Poly, it gives an example of inheritence and new instances.  RSS feed

 
Sea Poked
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.javaranch.com/campfire/StoryPoly.jsp

Lets say Dog is the subclass of Animal, therefore it inherits/extends (from) Animal.

Dog d = new Dog(); would produce an instance of dog under the variable name 'd'.

Animal d = new Dog(); would also produce an instance of dog under the same variable name?

Animal is the superclass. Does that mean if Animal inherits/extends from Organism, an 'Organism d = new Dog();' have the same effect as the first example?

Thanks.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use a variable of any superclass to store a reference to any subclass object. This is helpful when grouping objects together that have the same superclass (e.g. Animal array containing Dogs, Hippos, and Horses).

However, if you do Animal d = new Dog(); , d doesn't have access to anything from the Dog class, unless...

you cast d as a Dog, allowing functionality from the Dog class to be used.



Sea Poked wrote:http://www.javaranch.com/campfire/StoryPoly.jsp

Animal is the superclass. Does that mean if Animal inherits/extends from Organism, an 'Organism d = new Dog();' have the same effect as the first example?



Correct. The only difference is that now the methods from the Animal class aren't available without casting.
 
Sea Poked
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel Christophel wrote:
However, if you do Animal d = new Dog(); , d doesn't have access to anything from the Dog class, unless...

you cast d as a Dog, allowing functionality from the Dog class to be used.




But Animal d = new Dog(); DOES cast 'd' as an instance of 'dog'. Do you mean "Animal d;" (that creates a uhh.. instance of the animal class) must have "d = new Dog();" for 'd' to have functions from Dog?

I.e. d must be a new Dog for it to be... a new dog?
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sea Poked wrote:

But Animal d = new Dog(); DOES cast 'd' as an instance of 'dog'. Do you mean "Animal d;" (that creates a uhh.. instance of the animal class) must have "d = new Dog();" for 'd' to have functions from Dog?

I.e. d must be a new Dog for it to be... a new dog?


When you do Animal d = new Dog();, d does not know that it is pointing to a Dog object. It only knows it is pointing to an animal object. Since you know that d points to a Dog object, you can cast it without getting an error. Maybe look at this.
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sea Poked wrote:http://www.javaranch.com/campfire/StoryPoly.jsp

Lets say Dog is the subclass of Animal, therefore it inherits/extends (from) Animal.

Dog d = new Dog(); would produce an instance of dog under the variable name 'd'.

Animal d = new Dog(); would also produce an instance of dog under the same variable name?

Animal is the superclass. Does that mean if Animal inherits/extends from Organism, an 'Organism d = new Dog();' have the same effect as the first example?

Thanks.


Owing to your plan of inheritance, a dog is not only a dog but also an animal and an organism, while an animal is both
animal and organism.

So ,
Organism d = new Dog() means that d is pointing to an entity which is also organism too, so type compliance
is satisfied here...so java has no issues.

Manish
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!