Java / Interface
1. Are interfaces also inherited from Object class?
Interface types do not extend Object in source, but any interface instance is an Object at runtime. You can call Object methods on an interface reference.
2. What is a Marker(Tag) Interface?
An Interface which doesn't have any declaration inside (no methods) but still enforces a mechanism. Marker interfaces are also referred as Null interfaces.
3. What are the access modifiers applicable for the nested interface declared inside a class?
Inner interface (aka) nested interface can have any access modifier when declared inside a class.
4. Can an interface inherit/extend multiple interface?
Yes. package com.tutorials.interfaceExample; public interface MyInterface extends ParentInterface1, ParentInterface2 { void method(); } class ImplementingClass implements MyInterface { @ Override public void method() { System.out.println( "HI" ); } } interface ParentInterface1 { void method(); }
5. Advantages of Inner Interface.
promotes Encapsulation. Organise interfaces and facilitate logical grouping of related interfaces.
6. Example of a nested interface from Java API.
java.util.Map has inner interface Entry. public static interface Map.Entry<K,V>
7. Inner Interface in Java.
Inner interface also known as nested interface, refer to the interfaces that are declared inside an another interface or class.
8. Can an Interface implement another interface?
No. Interface doesn't provide implementation hence it is not possible.
9. Can a class be defined inside an Interface?
Yes. package com.javatutorials.accessModifer.protectedPackage; public interface MyMainInterface { public class MyClass { public static void method() { System.out.println( "Class method inside an interface."
10. Are Nested interfaces declared static?
Yes. Implicitly it is static.
11. can we have non-static inner interface?
No.
12. Syntax for inner interface.
An interface inside a interface. interface outer_interface_name { interface inner_interface_name { //static and public implicitly } } An interface inside a class.
13. Should the nested interface must be public?
Yes, when the nested interface is declared inside another interface. Because, there is no other way to use the nested interface, other than the external module.
14. Can we define a class inside an interface?
Yes, you can create both a nested class or an inner class inside a Java interface. interface InterfaceName { class ClassName { } }
15. Advantage of having a class inside an interface.
It limits the scope of the class to where it belongs. Class inside an interface is tightly coupled with the interface.
16. What is the difference between an Abstract class and Interface?
Abstract classes may have some executable methods and methods left unimplemented. Interfaces contain no implementation code....
17. Can a method inside a Interface be declared as final or static in Java?
No. An interface method can have only public, abstract, and static (from Java8) modifiers....
18. Can an Interface extend another Interface?
Yes.
19. Why interface is allowed to extend multiple interface whereas class can inherit only one class?
An interface adds contracts, not instance fields or constructors, so multiple superinterfaces do not create the diamond field problem a class hierarchy would.
20. Can an Interface be final?
No.
21. Give few examples of a marker interface?
Some of the marker interfaces are Serializable, Remote, Cloneable.
22. Can I define private and protected variables in an interface?
No.
23. What is Externalizable?
Externalizable is an Interface that extends Serializable Interface. It sends data into Streams in Compressed Format.
24. Difference between Serializable and Externalizable.
Serializable is a marker interface as it has defined no members while Externalizable is not as it has two methods.
25. Does Externalizable extends Serializable interface?
Yes.
26. Can you specify the accessibility modifier for methods inside an interface?
All the methods inside an interface are always implicitly public and abstract. public and abstract can be mentioned explicitly whereas other access modifier are not allowed.
27. What is abstract Interfaces in Java?
Interfaces marked with abstract modifiers are abstract interfaces. Every interface is implicitly abstract.
28. Can a interface define static initializer in Java ?
No. interfaces cannot have initializers.
29. What is functional interface in Java 8.
An interface with only one abstract method is known as functional interface. For example, Runnable and Callable interface are functional interface as it has only one method....
30. Why do we implement interfaces in Java?
Using interfaces keeps the use of chunks of functionality consistent. So when another class wants to use your class, it can act on it as a cloneable, disposable object without worrying about your implementation details.
31. What are the rules for writing lambda expressions in Java 8?
A lambda expression can have 0, 1 or more parameters . The parameter's type can be explicitly declared or it can be inferred from the context implicitly....
32. What is SAM interface in Java 8?
Functional Interface is also known as SAM Interface, it contains only one abstract method. SAM Interface stands for Single Abstract Method Interface.
33. How will you call a default method of an interface in a class?
Using super keyword along with interface name. interface A { default void foo () { System.out.println( "A.foo" ); } } public class B implements A { @Override public void foo () { System.out.println( "B.foo" ); } void aFoo () {...
34. Explain type inference in Java 8.
Type inference is a Java compiler's ability to look at each method invocation and corresponding declaration to determine the type argument that makes the invocation applicable. The inference algorithm determines the types of the arguments and, if available, the type...
35. Explain the difference between lambda expression and anonymous class in terms of this reference.
With an inner class, it creates a new scope. You can overwrite local variables from the enclosing scope by instantiating new local variables with the same names....
36. What is Optional in Java8?
Java 8 introducd Optional in java.util package that represents if a value is present or absent. The main advantage is No more too many null checks and NullPointerException....
37. Can we initialize Optional value as null in Java8?
Yes, but its a bad practice. The main intention of Optional is to eliminate null referencing.
38. Difference between Optional flatMap() and map() in Java8.
Use map if the function returns the object you need or flatMap if the function returns an Optional.
39. Does interface uses all Object class methods?
Yes. Although interfaces does not inherit from Object class, an interface implicitly declares a public abstract member method corresponding to each public instance method declared in the Object class....
40. Can interface have instance fields?
No instance fields. Interface fields are public static final. Java 8+ can add default and static methods; private methods exist from Java 9 for shared default code.
41. Can interface have instance method?
Yes, interface can have default methods from Java8 onwards.
42. How JVM invoke special behavior in case of marker interface?
Let consider Serializable interface as an example. Serialization is handled by the ObjectInputStream and ObjectOutputStream classes.
43. How to write your own marker interface?
We can create our own marker interface by creating an interface with no method. This marker interface has nothing to do with JVM, we add the special logic in market interface handler class by using instanceOf check....
44. Can we have private interface method?
Yes, private static interface methods are supported from Java 9 onwards. The default and the abstract methods cannot be private.
45. Can an interface have main method?
Yes, from Java8, interface allows static method. So we can write main method and execute it.
46. Can you modify the value of an interface field?
No . The interface fields are final and static implicitly.
47. Can we have private method/static method in Interface ?
Yes, from Java 9 onwards we can have private methods in interface.