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.
Ans. No. The whole idea of default method is to provide a default definition in case the implementing class intend to provide definition for only some of the methods. Making any of the interface method private would restrict it from being implemented by the implementing class. So default method is an option to provide implementation and not restricting a new definition.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Java 8 allowed for method implementation using default methods in interfaces. As those default methods could contain complex logic and might need organizing the logic into multiple methods, they have allowed for private methods.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Abstract Classes provide default implementations of methods that are inherited by the classes that extend them, which was not the case for Interfaces. This changed in Java 8, where default implementations are provided for methods.
Help us improve. Please let us know the company, where you were asked this question :
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 :
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 :
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 :
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.
Q13. 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(); } }
Ans. No. Compiler gives error saying "Duplicate Default Methods"
Help us improve. Please let us know the company, where you were asked this question :
Q14. 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(); } }
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 :
Q15. 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"); } }
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 :
Q16. 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 ?
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 :
Q17. 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 ?
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 :
Q18. 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 ?
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 :
Q19. Can we have default method with same name and signature in the derived Interface as the static method in base Interface and vice versa ?
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 :
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 :