Interface in Java Interview Questions and Answers for 10 years experience
-
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. Classes implement interfaces, providing concrete implementations for the methods declared in the interface. Interfaces support polymorphism and abstraction.
-
What is the difference between an interface and an abstract class?
- Answer: Key differences include: an interface can only have abstract methods and constants (since Java 8, it can also have default and static methods), while an abstract class can have both abstract and concrete methods. A class can implement multiple interfaces but can only extend one abstract class. Interfaces achieve complete abstraction, while abstract classes provide partial abstraction. Interfaces primarily focus on 'what' to do (contract), while abstract classes also handle 'how' partially (implementation).
-
Explain the use of the `extends` keyword with interfaces.
- Answer: The `extends` keyword is used in Java to create an interface that inherits from another interface. This allows for creating a hierarchical structure of interfaces, promoting code reusability and avoiding redundant declarations. An interface can extend multiple interfaces, inheriting methods from all of them.
-
Explain the use of the `implements` keyword.
- Answer: The `implements` keyword is used by a class to indicate that it is providing concrete implementations for the methods defined in one or more interfaces. A class must provide a body for all abstract methods declared in the interfaces it implements, unless it's also declared as abstract.
-
What is a marker interface? Give an example.
- Answer: A marker interface is an interface that doesn't contain any methods or constants. It serves as a tag to indicate that a class possesses a certain characteristic. `java.io.Serializable` is a classic example; implementing it marks a class as serializable.
-
Explain default methods in interfaces (Java 8 and above).
- Answer: Default methods provide a way to add new methods to existing interfaces without breaking code that implements them. They have a method body and are declared using the `default` keyword. Classes implementing the interface inherit the default method implementation but can override it if needed. This allows for evolution of interfaces without forcing changes across the whole codebase.
-
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 rather than instances of classes implementing the interface. They are declared using the `static` keyword and are accessed directly using the interface name. They are useful for utility methods related to the interface.
-
What is a functional interface? Give an example.
- Answer: A functional interface is an interface that contains exactly one abstract method. It can have any number of default, static, or override methods from Object class. `@FunctionalInterface` annotation is used to indicate that an interface is intended to be a functional interface. `java.util.function` package provides many examples such as `Runnable`, `Callable`, and `Predicate`.
-
How do interfaces support polymorphism?
- Answer: Interfaces enable polymorphism by allowing different classes to implement the same interface and provide their own specific implementations of the interface's methods. This allows you to treat objects of different classes uniformly through the interface type, enabling flexible and extensible designs.
Thank you for reading our blog post on 'Interface in Java Interview Questions and Answers for 10 years experience'.We hope you found it informative and useful.Stay tuned for more insightful content!