Interface in Java Interview Questions and Answers for 2 years experience

Java Interfaces: 100 Interview Questions & 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 defines 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: 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 (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 (static final variables).
  3. How do you declare an interface in Java?

    • Answer: Using the `interface` keyword followed by the interface name, e.g., `interface MyInterface { ... }`
  4. What is the purpose of the `implements` keyword?

    • Answer: The `implements` keyword is used by a class to indicate that it is fulfilling the contract defined by an interface. The class must then provide implementations for all abstract methods declared in the interface.
  5. Can an interface extend another interface?

    • Answer: Yes, an interface can extend one or more other interfaces using the `extends` keyword. This allows for inheritance among interfaces.
  6. Can a class implement multiple interfaces?

    • Answer: Yes, a class can implement multiple interfaces, allowing it to exhibit multiple behaviors.
  7. What is a marker interface? Give an example.

    • Answer: A marker interface is an interface that doesn't contain any methods. It serves as a tag to indicate that a class possesses a certain characteristic. `java.io.Serializable` is a classic example.
  8. Explain default methods in interfaces (Java 8 and above).

    • Answer: Default methods provide implementations for methods within an interface. This allows for adding new functionality to existing interfaces without breaking compatibility with classes that already implement them. They are declared using the `default` keyword.
  9. Explain static methods in interfaces (Java 8 and above).

    • Answer: Static methods in interfaces are similar to static methods in classes. They belong to the interface itself, not to any specific implementation. They are declared using the `static` keyword.
  10. What is a functional interface?

    • Answer: A functional interface is an interface that contains exactly one abstract method. It can have multiple default methods and static methods but only one abstract method. They are used extensively with lambda expressions.
  11. What is the `@FunctionalInterface` annotation?

    • Answer: It's an annotation used to indicate that an interface is intended to be a functional interface. It helps the compiler enforce the restriction of having only one abstract method and improves code readability.
  12. Explain the concept of polymorphism with interfaces.

    • Answer: Polymorphism, the ability of an object to take on many forms, is achieved with interfaces by allowing different classes to implement the same interface and provide their own specific implementations of the interface methods. This allows for flexibility and extensibility.
  13. Give an example of using an interface for loose coupling.

    • Answer: If class A uses interface I and class B implements I, then A doesn't need to know about B specifically. This reduces dependencies and makes the code more maintainable and flexible. Changes in B won't directly affect A as long as B continues to implement I correctly.

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