Java - Interview Questions and Answers on Interfaces

Q1.  What are Marker Interfaces ? Name few Java marker interfaces ?

Ans. These are the interfaces which have no declared methods.

Serializable and cloneable are marker interfaces.

Q2.  Is runnable a Marker interface ?

Ans. No , it has run method declared.

Q3.  Can we declare interface methods as private ?

Ans. No. 

Q4.  What is an Externalizable interface ?

Ans. Externalizable interface is used to write the state of an object into a byte stream in compressed format.

Q5.  Difference between serializable and externalizable interface ?

Ans. Serializable is a marker interface whereas externalizable  is not.

Q6.  What are the design considerations while making a choice between using interface and abstract class ?

Ans. Keep it as a Abstract Class if its a "Is a" Relationsship and should do subset/all of the functionality. Keep it as Interface if its a "Should Do" relationship.

Q7.  Whats the purpose of marker interfaces ?

Ans. They just tell the compiler that the objects of the classes implementing the interfaces with no defined methods need to be treated differently. 

Q8.  What will happen if class implement two interface having common method?

Ans. That would not be a problem as both are specifying the contract that implement class has to follow.
If class C implement interface A & interface B then Class C thing I need to implement print() because of interface A then again Class think I need to implement print() again because of interface B, it sees that there is already a method called test() implemented so it's satisfied.

Q9.  What is a cloneable interface and what all methods does it contain?

Ans. It is not having any method because it is a MARKER interface.

Q10.  What is Comparable Interface?

Ans. It is used to sort collections and arrays of objects using the collections.sort() and java.utils. The objects of the class implementing the Comparable interface can be ordered.