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.
Q92. 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 :
Q93. 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 :
Q94. 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 :
Q95. 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 :
Q96. 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 :
Q97. 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 :
Q100. 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. No. compareTo method is declared final for the Enumerations and hence cannot be overriden. This has been intentionally done so that one cannot temper with the sorting order on the Enumeration which is the order in which Enum constants are declared.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Static methods can be called using instance references wherein this would have made sense but static method can also be called using Class name wherein this would mean nothing and hence forbidden.
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 :
Ans. 1. public is the access modifier that makes the method accessible from anywhere, static is the keyword that makes it accessible even without creating any object, void means it doesn't return anything , String args[] is the array of argument that the method receives.
2. If we use main without the string args , it will compile correctly as Java will treat it as just another method. It wont be the method "main" which Java looks for when it looks to execute the class and hence will throw
Error: Main method not found in class , please define the main method as:
public static void main(String[] args)
3. Main is not a keyword but a special string that Java looks for while initiating the main thread.
Help us improve. Please let us know the company, where you were asked this question :
Ans. We are using Agile methodology. I attend daily stand up where the development leads takes the status of assigned stories, achievements, any bottlenecks or challenges. We follow iteration of 2 weeks.
Help us improve. Please let us know the company, where you were asked this question :
Q109. When are static and instance methods resolved ? During compile time or Runtime ?
Ans. Static methods are resolved during compile time and hence they cannot participate in runtime polymorphism. Instance methods are resolved during runtime.
Help us improve. Please let us know the company, where you were asked this question :
Q110. How can we execute a Java class independently if it doesn't have a static main method ?
a. By initiating the flow in any of the static method b. By initiating the flow in any of static block c. By initiating the flow in any other instance method named as main d. By initiating the flow in any other instance method named as main and making it final
Ans. By initiating the flow in any of static block
Help us improve. Please let us know the company, where you were asked this question :
Q111. Which of the following is false about main method ?
a. It should be declared public and static
b. it should have only 1 argument of type String array
c. We can override main method
d. We can overload main method
Ans. Parameters are the variables that the method is expected to receive along with the method call. Arguments are the values which are passed on while calling the methods.
Help us improve. Please let us know the company, where you were asked this question :
Ans. TDD is a development process that involves short iterations: first an automated test case is written. Then, the code is written to pass that test, and finally one refactors the new code to acceptable standards.
Help us improve. Please let us know the company, where you were asked this question :
Ans. It could be worthy to move a method to util class if the method needs to be shared, doesn't require polymorphic behavior and need not be overridden in special cases.
Don't belong to one group through is-a relationship ( You can share through parent class method )
Don't implement a specific interface ( java 8 default methods )
Doesn't involve complex computing as you will be loosing the benefit of object state with just static method.
Doesn't require polymorphic behavior as static methods don't participate in runtime polymorphism.
Help us improve. Please let us know the company, where you were asked this question :