• Post Reply Bookmark Topic Watch Topic
  • New Topic

basic inheritance question  RSS feed

 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand Java inheritance, when class B inherits from class A then class B will not only have all of A's variables and A's methods but in addition it will have all the new stuff in class B.
Since class B is also of type class A, that means wherever class A is passed as a parameter in some function then class B could also be passed as a parameter in that same function.

What I don't understand is since class B has a few new variables and/or methods that class A doesn't, how does the function class B is passed to know what to do with them?
After all, that function was set up to handle (the smaller) class A, so why doesn't the compiler complain about all the new stuff in class B?

Thanks,
Dan
 
Paul Clapham
Sheriff
Posts: 22836
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Bromberg wrote:As I understand Java inheritance, when class B inherits from class A then class B will not only have all of A's variables and A's methods but in addition it will have all the new stuff in class B.
Since class B is also of type class A, that means wherever class A is passed as a parameter in some function then class B could also be passed as a parameter in that same function.


Yes, that's correct. That's one of the primary features of inheritance.

What I don't understand is since class B has a few new variables and/or methods that class A doesn't, how does the function class B is passed to know what to do with them?
After all, that function was set up to handle (the smaller) class A, so why doesn't the compiler complain about all the new stuff in class B?


That method is expecting an A object, so it knows how to deal with an A object. It knows what its variables and methods are. So if you pass a B object instead, it treats it as if it were an A object (which it is). It doesn't know anything about the new stuff which B declared, and it doesn't (and can't) use it. You could say that the additional features are simply ignored.

And why should the compiler complain? The deal was only that it would get an A object. The fact that it gets an A object with some additional features which it isn't going to use isn't harmful in any way.
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great explanation, Paul. That's what I was hoping you would say. I imagine if I tried to use any of B's features in the function B was passed to then the compiler would complain.

Thanks again!
Dan
 
Paul Clapham
Sheriff
Posts: 22836
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's correct too.
 
Dan Bromberg
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul,
Thanks once again for your prompt and clear explanation.
Issue closed!
Dan
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!