Interface in Java Interview Questions and Answers for experienced

100 Java Interface Interview Questions and Answers
  1. What is an interface in Java?

    • Answer: An interface in Java is a reference type, similar to a class, that contains only constants and abstract methods. It specifies a contract that classes must adhere to if they implement the interface. Interfaces cannot be instantiated directly.
  2. What is the difference between an interface and an abstract class?

    • Answer: Key differences include: An abstract class can have both abstract and concrete methods, while an interface can only have abstract methods (before Java 8) and default/static methods (from Java 8 onwards). A class can extend only one abstract class but can implement multiple interfaces. Abstract classes can have instance variables, while interfaces can only have constants (implicitly public, static, and final).
  3. How do you declare an interface in Java?

    • Answer: The keyword `interface` is used. For example: `interface MyInterface { ... }`
  4. Explain the concept of multiple inheritance in Java using interfaces.

    • Answer: Java does not support multiple inheritance of classes to avoid the diamond problem. However, a class can implement multiple interfaces, effectively achieving multiple inheritance of behavior. This allows a class to inherit methods from multiple interfaces.
  5. What are marker interfaces? Give an example.

    • Answer: Marker interfaces are interfaces with no methods. They serve as tags or annotations to indicate that a class possesses a certain characteristic. `java.io.Serializable` is a classic example. Implementing it signals that objects of that class can be serialized.
  6. Explain the use of default methods in interfaces (Java 8 onwards).

    • Answer: Default methods provide a way to add new methods to existing interfaces without breaking compatibility with classes that already implement the interface. They are declared using the `default` keyword and provide a default implementation.
  7. Explain the use of static methods in interfaces (Java 8 onwards).

    • Answer: Static methods in interfaces are utility methods associated with the interface itself, not with any specific implementation. They are declared using the `static` keyword and are called directly on the interface.
  8. What is the purpose of the `Comparable` interface?

    • Answer: The `Comparable` interface defines a natural ordering for objects of a class. Implementing it allows objects to be compared using the `compareTo()` method, which is essential for sorting and other ordering operations.
  9. What is the purpose of the `Comparator` interface?

    • Answer: The `Comparator` interface allows defining custom comparison logic for objects. It provides the `compare()` method, which determines the order of two objects based on a specific criterion. This is useful when you need to sort objects using different criteria.
  10. Can an interface extend another interface?

    • Answer: Yes, an interface can extend one or more interfaces using the `extends` keyword. This allows for inheritance of methods and constants from other interfaces.
  11. What are the access modifiers allowed for methods in an interface?

    • Answer: Before Java 8, only `public` was implicitly allowed. From Java 8 onwards, `public`, `default`, and `static` are allowed.
  12. What happens if a class implements an interface and does not implement all its methods?

    • Answer: The class must be declared abstract. If it's not declared abstract, a compile-time error will occur.
  13. Explain the concept of functional interfaces.

    • Answer: A functional interface is an interface that has exactly one abstract method. It can have any number of default, static, or overridden methods from Object. They are heavily used with lambda expressions.
  14. Give examples of common functional interfaces in Java.

    • Answer: `Runnable`, `Callable`, `Comparator`, `Predicate`, `Function`, `Consumer`, `Supplier`.
  15. How do you use lambda expressions with interfaces?

    • Answer: Lambda expressions provide a concise way to implement functional interfaces. They are used to create anonymous implementations of functional interfaces.

Thank you for reading our blog post on 'Interface in Java Interview Questions and Answers for experienced'.We hope you found it informative and useful.Stay tuned for more insightful content!