4

If class a extends class b, and class b extends class c, does a inherit from both c and b? If so, in case of conflict between the 2, is there an error or does one override another (and if so, which one)?

Please rename the question if it is unsuitably titled.

EDIT: what I mean by conflict is something like the following:

class c {
   int foo;
   //Stuff
}
class b extends c {
   String foo;
   //Stuff
}
class a extends b {
   //Stuff
}

Is a.foo a string or an int?

Another edit: So from what I gather, inheritance here is a bit like CSS - the closer the rule is set, the larger priority it has (e.g. inline styles override stylesheets). Is that a good way of considering this, or is it significantly different?

4
  • 2
    Tangentially, this is why public and protected variables are considered poor form by many. Commented Jun 24, 2012 at 14:02
  • @TonyEnnis - neither; I'm just trying to extend two "library" classes into one. Commented Jun 24, 2012 at 14:28
  • If there is a real conflict the code won't compile. There is no 'conflict' in your post, just a simple name hiding issue that you could have tested for yourself. StackOverflow is not a research assistant. Downvote. Commented Jun 24, 2012 at 23:08
  • I was talking about more advanced cases, and whether anything will change. Would you like me to generate every single case of a conflict such as this? I'm sure that a program written to do that might have some class conflicts of its own :P Commented Jun 25, 2012 at 19:07

6 Answers 6

5

If something exists in both b and c, a will inherit whichever one b uses.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, but if only you had added "...and so forth" to the end, it would have been perfect.
4

Yes, a inherits from b and c. I don't see how there can be a conflict in this situation since b's method overrides will be valid for a. You may be thinking of the diamond problem that comes from conflicts from multiple inheritance, but that's when you have two different parents, not two parents with one parent being the parent of the other.

You can run into a "soft" diamond problem with interfaces, where two methods have the same signature, but the rules for one method don't match those of another. Since neither interface has an implementation for their methods, there is no compilation problem (that I know of), just a logical problem.

Edit Ah, I've seen your edit regarding variables, and I agree with Jimpanzee's response to it. It's certainly easy to test:

public class Test3 {
   public static void main(String[] args) {
      MyA myA = new MyA();
      System.out.println("foo := " + myA.foo);
   }
}

class MyC {
   public int foo = 3;
   //Stuff
}
class MyB extends MyC {
   public String foo = "foo";
   //Stuff
}

class MyA extends MyB {

}

Comments

2

well, the rule is as follows.

  1. Any subclass will inherit nearest up its hierarchy. so a will get everything from b. Because everything which is on inheritance stack will be available till b. as b had inherited already everything from its inheritance hierarchy.

  2. Secondly, if b over-rides anything(instance variable/method), then a will see over-ridden version. So in your case a will get String foo.

5 Comments

Can you put in a link to the JLS for this?
Well..that's what crux of Inheritance is..I'll pleased if you find any conflicting matter
No, I agree with you. I was just suggesting a JLS link for the benefit of the original poster. 1+ up-vote.
No, there is no overriding of anything except methods in Java. Everything else is just inherited, same name or not.
@Marko...It is technically called Instance Variables Hiding
2

This answer has a much better example, and points out that doing this sort of thing is considered bad practice. (Eclipse will give you a warning, for example.) Extending your code a bit:

class c {
   int foo = 42;
   //Stuff
}
class b extends c {
   String foo = "foostr";
   //Stuff
}
class a extends b {
   //Stuff
}
class Main{
   public static void main(String[] args){
      a mya = new a();
      System.out.println(mya.foo);
   }
}

Running java Main prints foostr.

Comments

2

In Java only methods are subject to overriding. Everything else is just inherited and there is only the issue of a namespace clash, but everything is still accessible. In your example both foos are accessible in A (I've corrected the class names to make them conform to the strong Java naming conventions):

class C {
  int foo;
  //Stuff
}
class B extends C {
  String foo;
  //Stuff
}
class A extends B {
  String x = ((B)this).foo;
  int i = ((C)this).foo;
}

Comments

1

Yes, it inherits from both c and b. In order to prevent/avoid conflicts, Java support a single hierarchy model (differently from other OOP languages like C++ which allow multiple-class inheritance model).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.