• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inheritance  RSS feed

 
lahiru nanayakkara
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


In here "a1" also keeps address of B object.so why can't assign "a1" to "b1" directly(without casting)
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give your classes real names rather than A and B and it will become obvious.
public class Animal … public class Dog extends Animal

If you now try to assign an Animal object to a Dog reference, how do you know the Animal is not a Cat?
Read about widening and narrowing reference conversions in the Java Language Specification, but that can be difficult to read.
 
Robert D. Smith
Ranch Hand
Posts: 221
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might help as well. Campfire Story And it's a lot more readable than the language spec.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robert D. Smith wrote: . . . it's a lot more readable than the language spec.
That is no great achievement
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!