• Post Reply Bookmark Topic Watch Topic
  • New Topic

Multiple Inheritance problem persists in Interfaces  RSS feed

 
Suvojit Chakraborty
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I tentatively made a program and found that multiple inheritance problem of C++ persists even with interfaces. Although this is definetely a special case but I want to know what is this problem known as( i know that this is perhaps known as diamond problem in C++). And is there a way out of this thing.


Output

D:\Education\Java\JavaStudyRoom\Applets>javac xyz.java
xyz.java:16: reference to i is ambiguous, both variable i in one and variable i
in two match
System.out.println(i);
^
1 error


Thanks for replying
 
Dave Lorde
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disambiguation - it's the same solution as in C++ : you have to specify which of the constants you want to use:Incidentally, you'll find it a lot easier to follow code if you use the Java naming conventions - type names begin with an uppercase letter, method and variable names begin with a lowercase letter.
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the error message is clear: which i do you want? Dave's solution removes the ambiguity.
 
Suvojit Chakraborty
Ranch Hand
Posts: 66
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Lorde wrote:Disambiguation - it's the same solution as in C++ : you have to specify which of the constants you want to use:Incidentally, you'll find it a lot easier to follow code if you use the Java naming conventions - type names begin with an uppercase letter, method and variable names begin with a lowercase letter.



Oh i see. So we take the same old C++ approach.
But I want to ask, why do we then say that Java has overcome the diamond problem of C++ by abandoning multiple inheritance. We still can end up in such situations in Java.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C++ this conflict also applies to implementation. That's what Java prevents, since interfaces include the method protocols only and cannot contain method implementation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!