Java - Interview Questions and Answers for 'Default method' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2137 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Default method' - 17 question(s) found - Order By Newest

 Q1. Why can't we have diamond problem with interfaces ?Core Java2017-07-30 08:15:33

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


 Q2. If there is a conflict between Base Class Method definition and Interface Default method definition, Which definition is Picked ?
Admin
info@buggybread.com
Ans. Base Class Definition.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods


Rarely asked as default methods have been introduced with Java 8.
 Q3. Can we have a default method without a Body ?Core Java
Admin
info@buggybread.com
Ans. No. Compiler will give error.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default method   yes-no        rare


 Q4. Does java allow implementation of multiple interfaces having Default methods with Same name and Signature ?Core Java
Admin
info@buggybread.com
Ans. No. Compilation error.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default method   yes-no     cognizant (CTS)      intermediate


  Q5. What are Default Methods ?Core Java
Admin
info@buggybread.com
Ans. With Java 8, We can provide method definitions in the Interfaces that gets carried down the classes implementing that interface in case they are not overridden by the Class. Keyword "default" is used to mark the default method.

  Sample Code for interface default

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods     DataArt  Expeditors  Wipro  GoodData  Infosys Technologies  sharp software development  Ensure support services      expert        frequent


Rarely asked as default methods have been introduced with Java 8.
 Q6. Can we have a default method definition in the interface without specifying the keyword "default" ? Core Java
Admin
info@buggybread.com
Ans. No. Compiler complains that its an abstract method and hence shouldn't have the body.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods   default keyword   yes-no


 Q7. Can a class implement two Interfaces having default method with same name and signature ?

public interface DefaultMethodInterface {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface");
}
}

public interface DefaultMethodInterface2 {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface2");
}
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface defMethIn = new HelloJava8();
defMethIn.defaultMethod();
}
}
Admin
info@buggybread.com
Ans. No. Compiler gives error saying "Duplicate Default Methods"

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods


 Q8. What If we make the method as abstract in another Interface ?

public interface DefaultMethodInterface {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface");
}
}

public interface DefaultMethodInterface2 {
public void defaultMethod(){
System.out.println("DefaultMethodInterface2");
}
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface defMethIn = new HelloJava8();
defMethIn.defaultMethod();
}
}
Admin
info@buggybread.com
Ans. Even then the Compiler will give error saying that there is a conflict.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods


 Q9. What if we override the conflicting method in the Class ?

public interface DefaultMethodInterface {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface");
}
}

public interface DefaultMethodInterface2 {
default public void defaultMethod(){
System.out.println("DefaultMethodInterface2");
}
}

public class HelloJava8 implements DefaultMethodInterface,DefaultMethodInterface2 {
public static void main(String[] args){
DefaultMethodInterface defMethIn = new HelloJava8();
defMethIn.defaultMethod();
}

public void defaultMethod(){
System.out.println("HelloJava8");
}
}
Admin
info@buggybread.com
Ans. There won't be any error and upon execution the overriding class method will be executed.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. What will happen if there is a default method conflict as mentioned above and we have specified the same signature method in the base class instead of overriding in the existing class ?
Admin
info@buggybread.com
Ans. There won't be any problem as the Base class method will have precedence over the Interface Default methods.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods


 Q11. If a method definition has been specified in Class , its Base Class , and the interface which the class is implementing, Which definition will be picked if we try to access it using Interface Reference and Class object ?
Admin
info@buggybread.com
Ans. Class method definition is overriding both the definitions and hence will be picked.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods


 Q12. If a method definition has been specified in the Base Class and the interface which the class is implementing, Which definition will be picked if we try to access it using Interface Reference and Class object ?
Admin
info@buggybread.com
Ans. Base Class Definition will have precedence over the Interface Default method definition.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods


 Q13. Can we have default method with same name and signature in the derived Interface as the static method in base Interface and vice versa ?
Admin
info@buggybread.com
Ans. Yes , we can do that as static methods are not accessible using references and hence cannot lead to conflict. We cannot do inverse as Default methods cannot be overridden with the static methods in derived interface.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   default methods   static interface methods


Not frequently asked as it was introduced with Java 8.
 Q14. What is the @FunctionalInterface annotation ?Core Java
Admin
info@buggybread.com
Ans. This is an informative annotation that specify that the interface is a functional interface. A Function Interface has only one abstract method and many default methods. Compiler generates an error if the interface specified with the annotation doesn't abide by the specifications for functional interface.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   java8   java 8   functional interface   default methods


 Q15. What was the driving force to introduce default methods in Interfaces wef from Java 8 ?Core Java2016-07-14 14:36:49

Ans. Earlier any class implementing an interface was supposed to implement all methods declared in an interface. There was no place for optionally implementing all or subset of methods.Though we have abstract classes wherein we could have provided such a mechanism by declaring some methods as abstract while providing definition for some. But as Abstract classes have a body and are comparatively heavier than interfaces and interfaces associate closely to the concept of providing interfacing than abstract classes, Java might have though of providing optional implementation for default methods. This way same interface can be reused in variety of ways rather than making copies of an interface to suit different needs.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java 8   java8  interface default methods  default methods     Infoview Technologies  Oracle  Oracle Polska  Infosys  Tata Consultancy (TCS)  CEBS Worldwide


 Q16. Does default methods introduce multiple inheritance and the Diamond problem in Java 8 ?Core Java2016-07-20 17:16:28

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  diamond problem   interfaces


 Q17. What are the advantage of Abstract classes over interfaces with respect to Java 7 ? and What changed in Java 8 to help facilitate that in Java 8 ?Core Java2017-08-17 19:28:33

 This question was recently asked at 'Proofpoint'.This question is still unanswered. Can you please provide an answer.


 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     abstract classes  interfaces  default method     Proofpoint      expert




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: