Java Interface and Diamond problem

Java said to be solving the diamond problem using interface.
that is, look into the following program,



Now if you create a new class D extending Class B and C and call the show() method using the object of D, the compiler won’t know which method to call. i.e the show() method of class B or C. So Java does not support multiple inheritance for classes.

So in java the above problem is rectified by using interface.



Now it solved the diamond problem for methods. But still it did not solved diamond problem for variables. See the following program,



Now there is a ambiguous call for the variable i. The compiler don’t know which variable to call. i.e The variable i in Interface B or C.

3 comments:

Adarsh Thimmappa said...

Good article!

Unknown said...

Excellent article. Thank you.

Kiran said...

for the last program you can use interface's name to refer so there is no ambiguity...btw nice article man.

Post a Comment

 
Copyright © Arun's Blog
Design by Arunkumar