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 'Multiple inheritance' - 6 question(s) found - Order By Rating | |||||||||||
| |||||||||||
Ans. No | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  abstract class   multiple inheritance  object oriented programming (oops)  oops concepts Asked in 2 Companies | |||||||||||
| |||||||||||
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. Multiple Inheritance refers to the concept of a class inheriting multiple classes. Example - Class C extends Class A ,Class B. This is not allowed in Java. Multilevel Inheritance refers to the concept of Inheritance in a chain. Example - Class B extends Class A, Class C extends Class B. This is permitted in Java. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  inheritance  object oriented programming (oops)  oops concepts   multiple inheritance  object oriented programming (oops)  oops concepts   multi level inheritance  object oriented programming (oops)  oops concepts 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 | |||||||||||
| |||||||||||
Ans. That would not be a problem as both are specifying the contract that implement class has to follow. If class C implement interface A & interface B then Class C thing I need to implement print() because of interface A then again Class think I need to implement print() again because of interface B, it sees that there is already a method called test() implemented so it's satisfied. | |||||||||||
Help us improve. Please let us know the company, where you were asked this question : | |||||||||||
Like Discuss Correct / Improve  java   oops   interfaces   multiple inheritance | |||||||||||
| |||||||||||
Ans. There was no multiple inheritance in java before version 8 as implementing multiple interfaces cannot be termed as inheritance. With Java 8 , came the concept of default methods wherein the class implementing the interface carries the default implementation from the interface and hence facilitating multiple inheritance. | |||||||||||
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   inheritance  object oriented programming (oops)  oops concepts   oops concept Asked in 10 Companies   frequent | |||||||||||
| |||||||||||