File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Queston about Inheritance Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Queston about Inheritance" Watch "Queston about Inheritance" New topic

Queston about Inheritance

howie jao

Joined: Nov 08, 2009
Posts: 14
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

Joined: Oct 01, 2001
Posts: 2968

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

Joined: Nov 08, 2009
Posts: 14
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

Joined: Oct 28, 2009
Posts: 33
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

Joined: Nov 08, 2009
Posts: 14
Thank you Bert for you response. I personally agree that. I am just wondering if there is trick to that. =)
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46402
You would find it easier to understand if you drew UML class diagrams on a sheet of paper.
Vlad Varnica
Ranch Hand

Joined: Dec 28, 2002
Posts: 40
Here it is

[Thumbnail for inheritence.png]

I agree. Here's the link:
subject: Queston about Inheritance
It's not a secret anymore!