• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Queston about Inheritance

 
howie jao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I have an question about inheritance. I am wondering how can I achieve below situation.
Class C extend Class B
Class B extend Class C

I want a class that can extend from B only . not A . Not C .

 
Greg Charles
Sheriff
Posts: 2985
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't have C extends B and B extends C. You can have a class that extends B only, not A or C. Could you be clearer about what you want to do?
 
howie jao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry that I have not been clear.

If currently I have 3 classes: A, B, C .
C extend B
B extend A

Is is possible to have a new class ,ie: D that only extend B but not A or C ?
Because I know that if this new class extend B then it will extend A . is thers a way that this can be happening. Just curiosity.
 
Bert Wilkinson
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you "extend" a class, you automatically extend everything ABOVE IT in the chain. So, in your example, if D extends B it would also extend A by definition, but it would not, and cannot extend C.

if you're contemplating complicated extends relationships, you might want to research interfaces, which are easier with lateral relationships.

 
howie jao
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Bert for you response. I personally agree that. I am just wondering if there is trick to that. =)
 
Campbell Ritchie
Sheriff
Pie
Posts: 48935
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would find it easier to understand if you drew UML class diagrams on a sheet of paper.
 
Vlad Varnica
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here it is
inheritence.png
[Thumbnail for inheritence.png]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic