Interface in Java Interview Questions and Answers
-
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. A class implements an interface, promising to provide implementations for all the methods declared in the interface.
-
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 (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 (static final variables).
-
How do you declare an interface in Java?
- Answer: You use the `interface` keyword followed by the interface name, enclosed in curly braces `{}` containing the methods and constants.
-
Can an interface have constructors?
- Answer: No, interfaces cannot have constructors. They are not instantiated directly.
-
Can an interface have instance variables?
- Answer: Technically, yes, but implicitly they are `public static final`. You don't need to explicitly declare these keywords.
-
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 between interfaces.
-
Can a class implement multiple interfaces?
- Answer: Yes, a class can implement multiple interfaces using the `implements` keyword.
-
What is a marker interface? Give an example.
- Answer: A marker interface is an interface with no methods. It serves as a tag to indicate that a class possesses a certain characteristic. `java.io.Serializable` is a classic example.
-
What is the purpose of the `Comparable` interface?
- Answer: The `Comparable` interface provides a way to compare objects of a class. It has a single method, `compareTo()`, which returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
-
What is the purpose of the `Iterator` interface?
- Answer: The `Iterator` interface allows you to traverse a collection of objects, one element at a time, without exposing the underlying implementation of the collection.
-
Explain default methods in interfaces (Java 8 and above).
- Answer: Default methods provide a way to add new methods to existing interfaces without breaking existing implementations. They are declared using the `default` keyword. Classes implementing the interface don't need to provide an implementation for default methods unless they want to override them.
-
Explain static methods in interfaces (Java 8 and above).
- Answer: Static methods in interfaces are similar to utility methods. They belong to the interface itself, not to any specific implementation. They are called directly using the interface name.
-
What is the significance of the `@FunctionalInterface` annotation?
- Answer: The `@FunctionalInterface` annotation indicates that an interface is intended to be a functional interface—an interface with exactly one abstract method. It helps to ensure that the interface adheres to the functional interface contract and aids in compile-time checking.
-
Explain the concept of polymorphism with respect to interfaces.
- Answer: Polymorphism allows objects of different classes that implement the same interface to be treated as objects of that interface type. This enables flexibility and code reusability.
-
What are some best practices for designing interfaces in Java?
- Answer: Keep interfaces small and focused. Avoid adding unnecessary methods. Use descriptive names for interfaces and methods. Prefer interfaces over abstract classes when possible for achieving loose coupling.
-
Can an interface inherit from a class?
- Answer: No, an interface cannot inherit from a class. It can only extend other interfaces.
-
Can a class extend a class and implement an interface at the same time?
- Answer: Yes, a class can extend one class and implement multiple interfaces. This is a common pattern in Java.
Thank you for reading our blog post on 'Interface in Java Interview Questions and Answers'.We hope you found it informative and useful.Stay tuned for more insightful content!