Search Interview Questions | More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know. |
|
| ||||
Core Java - Interview Questions and Answers for 'Diamond problem' - 3 question(s) found - Order By Newest | ||||
| ||||
Ans. Interfaces don't have member elements and method definitions that could cause diamond problem. With Java 8, Interfaces have default method definitions. This could have created diamond problem but Java introduced a compile time check for "duplicate default methods" in case same method is derived from multiple interfaces and no definition is overridden by the class. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  diamond problem  interfaces  java 8  default methods | ||||
Related Questions | ||||
| ||||
Ans. class A { void test() { System.out.println("test() method"); } } class B { void test() { System.out.println("test() method"); } } Suppose if Java allows multiple inheritance like this, class C extends A, B { } A and B test() methods are inheriting to C class. So which test() method C class will take? As A & B class test() methods are different , So here we would Facing Ambiguity. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  multiple inheritance  object oriented programming (oops)  oops concepts  diamond problem Asked in 20 Companies basic   frequent | ||||
| ||||
Ans. Default methods results in multiple inheritance of behavior and not of state. In case we try to implement multiple interfaces with default method having same name and signature, and don't override it in implementation class, it will throw an error. For example - interface MyInterface { public void default myMethod(){ } } interface MyInterface2 { public void default myMethod(){ } } class MyClass implements MyInterface,MyInterface2 { } This code will compilation error "Duplicate Default Method" if we specify the definition of myMethod() in myClass, compiler won't complain and there is no conflict and MyClass can use overridden definition. But if we don't override myMethod() in MyClass, Java would be in conflict as to what definition should be carried to MyClass and hence throws compilation error. | ||||
Help us improve. Please let us know the company, where you were asked this question : | ||||
Like Discuss Correct / Improve  default methods  java 8  multiple inheritance  object oriented programming (oops)  oops concepts  diamond problem   interfaces | ||||
Related Questions | ||||
Why can't we have diamond problem with interfaces ? | ||||
Does java allow implementation of multiple interfaces having Default methods with Same name and Signature ? | ||||
What are Default Methods ? | ||||
What was the driving force to introduce default methods in Interfaces wef from Java 8 ? | ||||
Can we have a private default method ? | ||||
If there is a conflict between Base Class Method definition and Interface Default method definition, Which definition is Picked ? | ||||
Can we have a default method without a Body ? | ||||
Can we have a default method definition in the interface without specifying the keyword "default" ? | ||||
Can a class implement two Interfaces having default method with same name and signature ? | ||||
What If we make the method as abstract in another Interface ? | ||||