Interfaces are among the common topics discussed in java interview while most of the candidates know what an interface is and have used it several times but they fail to answer some surrounding interfaces which goes against them. The aim of this article is to list those questions .If you encountered some question oon interfaces which is not in this list feel free to submit it in comments or mail us
- Difference between an abstract class and interface in java
- Reffer this article.
- What all an interface can contain ?
Method declarations (signature) and constant declarations. All constant values defined in an interface are implicitly public, static, and final. You can omit these modifiers
- Why are interface variables static and final by default in Java?
Interface variables are static because Java interfaces cannot be instantiated in their own right; the value of the variable must be assigned in a static context in which no instance exists. The final modifier ensures the value assigned to the interface variable is a true constant that cannot be re-assigned by program code.or to put it simply anything else is part of the implementation, and interfaces cannot contain any implementation
- Can you instantiate an interface ?
- Can a class implement multiple interfaces ?
YES a class can implement multiple interfaces but it cannot inherit multiple abstract classes since multiple inheritance is not there in java.
- When is an abstract class favoured over interface?
Since it’s almost impossible to add a new method on a published interface, it’s better to use abstract class, when evolution is concern. Abstract class in Java evolves better than interface. Similarly, if you have too many methods inside interface, you are creating pain for all it’s implementation, consider providing an abstract class for default implementation.
Refer this article to see common interview questions on Abstract Class .