Wednesday, June 17, 2015

Top Java Interface Interview Questions

Interface questions are asked mostly in mid level to senior level interviews (3-8 yrs), with increasing complexity. Given that interfaces are base of most applications which help the code loosely couple and modular. This doesn't come as a surprise. 

In the projects that I have worked, Interface was the starting point of our design and the whole code was written on top of those interfaces, making code loosely coupled, scalable and easy to manage.

Patterns based on interface (Factory/Abstract Factory) are another hit among the interviewers. So is the difference and usage of abstract class and interface. Let's go through expected questions one by one. 

1. Explain and interface.

Interface is a defined just like a class. Only that the methods are defined but not implemented. As for the member variable, interface can declare constants. A class that implements and interface has to implement all the methods of an interface. It forms a 'like-a' relationship.

2. Given everything is same, what do you prefer Abstract class or interface?

Without much information about the problem, it is difficult to answer. But interface should be the preferred thing. As a class can only extent 1 abstract class, but implement multiple interfaces. This makes the code flexible.

3. Can we add a new method in an interface written long back in legacy code?

It's not as straightforward as writing a new function. Since all the classes that are implementing the interface will break unless they all implement the interface (be it empty implementation). Hence interfaces should be designed properly at the very beginning. Later on we have to more or less stick with what we have. 

We can theoretically do it and implement the function in all classes that have already implement the interface. But it is avoidable.

4. What is the difference between abstract class and interface?

Be sure to get this question for junior level interviews.

Abstract Class
Contains either abstract method or concrete methods
Contains only method declaration. No concrete methods allowed.
Class which extends abstract class, need not define all methods
Compulsory for implementing to override all methods
Used when we want to define general features at one place with each extending class defining specialization
Used when we want to define all behavior type at a single place, with implementing unique way of same behavior type
Variables can be defined as suitable
Variable are public static final by default
Fast processing as tightly coupled
Loosely coupled. Hence processing is a bit slow.

5. Interface/Abstract class. When to use what?

As we read, it's not feasible to add a method to an interface later on but can be added to an abstract class without issues. Hence interface should be avoided in cases where we want an evolving code. Also in situations when we have a lot of methods inside an interface, it becomes a pain to implement each one. We should prefer abstract class in such cases.

6. Abstract Class implementing an Interface. What would be the behaviour?

In such a case, abstract class need not implement all methods of the interface as it is abstract.

7. Can interface have a constructor?

No interface can't have a constructor.

8. What is List/Map Interface?

List Interface provides ordered collection, We can add/get values using index.
Map maps keys to values. Duplicate keys are not allowed.

A lot of implementation of List and Map are provided with Java.