Q1. What are Default Methods ?
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.
Q2. Can we have a default method definition in the interface without specifying the keyword "default" ?
Ans. No. Compiler complains that its an abstract method and hence shouldn't have the body.
Q3. 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"
Q4. 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.
Q5. 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.
Q6. 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.
Q7. If there is a conflict between Base Class Method definition and Interface Default method definition, Which definition is Picked ?
Ans. Base Class Definition.
Q8. 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.
Q9. 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.