Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

changing reference types

 
Neha Mittal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does this mean -> "Although a reference variable's type can't be changed, it can be used to refer to an object whose type is a subtype of its own" ?
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have a reference to a Vehicle. You can use that reference to point to a Car object or a Bus object.
 
Aum Tao
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is referring to inheritance. Consider the following example:


Taking your case, the Animal reference can be assigned to the Dog object, since it satisfies the IS A relationship. However, this fails for the second assignment since Shape doesn't extend Animal. Moreover, it shouldn't since they are not logically related.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic