No comments yet

Top 10 Interview Questions in Interfaces in Java 8

[Social9_Share type="horizontal"]

Top 10 Interview Questions in Interfaces

In this blog lets discuss about the interview questions relevant to Interfaces

  1. What is an interface?
  2. An interface is contract which specifies the behavior that a class must implement. The purpose of an interface is to provide multiple implementations to the class. An interface in Java 8 can have abstract methods, static methods and default methods.

    Know more about interfaces

    • An interface can have abstract, default and static methods.
    • The variables in an interface are public,static, final
    • A class can implement multiple interfaces – to get multiple implementations. Refer question-2
    • Multiple classes can implement an interface to be grouped under a single category. Refer question-3
    • An interface with one abstract method is called Functional Interface. Refer question-4
    • An interface without methods is called as Tag/marker interface. Refer question-5
    • Interfaces can be extended. Refer question-7
    • When a class implements an interface, all the methods in the interface should be implemented in the class. If any of the methods are not implemented, make the class abstract, extend it and implement the method in the subclass



    Let us see an example for interface and how to call the methods. We will use the above given interface BonusCalculator. Now, we will create a class Manager that implements the interface. Create an interface reference and point to the Manager object. Then call the methods using the interface reference. You can call the static method only using the interface name.(You cannot call using the interface reference also).


  3. What is the use of a class implementing multiple interfaces?
  4. The reason for a class implementing multiple interfaces is to get multiple functionality.
    eg.class Employee implements BonusCalculator, IncCalculator{ }
    Now, Employee class gets the functionality of two interfaces.

  5. Why should multiple classes implement an interface?
  6. Multiple classes can implement an interface – to be grouped under a category. Lets say we have two classes Employee and Student. Both the classes implement Serializable. It means the object of these classes can be serialized. So, they become Serializable classes(grouping different classes into one category)
    eg.class Employee implements Serializable{ } class Student implements Serializable{}

    Become an expert Java Developer

  7. What is a Functional Interface?
  8. A functional interface is an interface with only one abstract method. But it can have default and static methods. You can also annotate this interface with @FunctionalInterface, though not mandatory. This annotation helps to check in the compile time if this is a functional interface.
    Examples: java.lang.Comparable, java.lang.Runnable, . Java 8 has introduced a lot of functional interfaces to work with Lambda expressions. Learn about lambda here Function, Predicate,Consumer, Supplier, BiPredicate, BiConsumer are few examples of functional interfaces introduced in Java 8.


  9. What is a marker interface?
  10. A marker interface is an interface without methods. It is also called as Tag interface. The purpose of this marker interface is to group multiple classes under one category. is a marker interface. The classes that implement this interface Serializable can serialize their objects. Similarly java.lang.Cloneable is a marker interface. The classes that implement Cloneable can clone/duplicate their objects.

  11. What is the use of extending an interface?
  12. Whenever you want to add new abstract methods in an existing interface without breaking the contract, it is not possible. But you can extend the existing interface and add the new methods. Create a class that implements the sub interface to call these methods.


  13. What are default methods and what is the use of them?
  14. When you want to add new functionality to an existing interface without breaking the contract, then you can use default methods. For creating default methods, use default keyword and add definition for the method.
    For example, lets say, we have three classes A, B, C implementing an interface Checker. Now if we want to add abstract methods to the Checker interface, it is not possible. One way is to extend this interface as shown above or use default methods. Default methods also support backward compatibility.To learn about default methods in detail click here.
    Let us modify the previous example using default methods.


  15. Can we override default methods?
  16. Yes. We can override default methods. To call the default method of the interface from within the overridden method of the implementing class, use interfaceName.super.methodName()


    One other usecase scenario is, if a class(Employee) implements two interfaces having same default method names, then it will give a compiler error. In this case, you should override the default method in the implementing class

  17. What is the use of static methods?
  18. Static methods in an interface are similar to default methods. When you want to add new functionality to an existing interface without breaking the contract, then you can use static methods. But it cannot be overridden.

  19. What is the difference between abstract and interface? When to use what?
  20. Abstract classes can have both abstract and concrete methods. A class can extend only one abstract class.
    Interfacesin java 8 can have abstract, default and static methods. A class can implement multiple interfaces.

    When to use abstract classes?
    In a java application having multiple classes, if the class needs its own properties(instance variables), then use abstract class, so that the subclasses can use the properties. For Example, Vehicle, Car, Bike has properties as model, price, color. If there is IS-A relationship between classes, then use abstract classes. Car IS-A Vehicle.

    When to use interfaces?
    In a java application having multiple classes, if the class does not have IS-A relationship on other classes, but wants to use the functionality, then use interfaces.The classes can implement many interfaces to get multiple functionality.For Example, an interface Insurance can be used by Car, Bike. There is no IS-A relationship. This is complete abstraction.

    Thats all.
    Happy Learning

Post a comment