Interface in Java Interview Questions and Answers for freshers
-
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; they serve as blueprints for classes.
-
What is the purpose of an interface?
- Answer: Interfaces promote abstraction, polymorphism, and loose coupling. They define a set of methods that classes must implement, enforcing a consistent behavior across different classes. This improves code maintainability, reusability, and extensibility.
-
How do you declare an interface in Java?
- Answer: You declare an interface using the `interface` keyword followed by the interface name, and enclosed within curly braces `{}`. For example: `interface MyInterface { ... }`
-
Can an interface have instance variables?
- Answer: Yes, but only if they are declared as `static final`. These are effectively constants.
-
Can an interface have constructors?
- Answer: No, interfaces cannot have constructors. They cannot be instantiated.
-
What is the difference between an abstract class and an interface?
- Answer: An abstract class can have both abstract and concrete methods, instance variables, and constructors. An interface can only have constants and abstract methods (before Java 8). A class can extend only one abstract class but can implement multiple interfaces. Interfaces are more purely abstract than abstract classes.
-
Explain the concept of multiple inheritance in Java using interfaces.
- Answer: Java doesn't support multiple inheritance of classes to avoid the diamond problem. However, it supports multiple inheritance of interfaces. A class can implement multiple interfaces, inheriting the methods from all of them.
-
How do you implement an interface in a class?
- Answer: Use the `implements` keyword followed by the interface name. The class must then provide implementations for all the abstract methods declared in the interface. For example: `class MyClass implements MyInterface { ... }`
-
What happens if a class implements an interface but doesn't implement all its methods?
- Answer: The class must be declared as abstract. Otherwise, the compiler will generate an error.
-
What are default methods in interfaces (Java 8 onwards)?
- Answer: Default methods in interfaces provide a default implementation for a method. This allows you to add new methods to an existing interface without breaking compatibility with classes that already implement it.
-
How do you resolve conflicts if two interfaces have the same method signature?
- Answer: The implementing class must explicitly override the method and provide its own implementation. Otherwise, a compile-time error will occur.
-
What are static methods in interfaces (Java 8 onwards)?
- Answer: Static methods in interfaces are associated with the interface itself, not with any specific implementation. They are called directly using the interface name.
-
What are private methods in interfaces (Java 9 onwards)?
- Answer: Private methods in interfaces provide helper methods that can be used by other methods within the interface but are not accessible from the outside or implementing classes. They promote code reusability and encapsulation within the interface.
-
Explain the concept of a marker interface.
- Answer: A marker interface is an interface that has no methods. It serves only as a tag to indicate that a class belongs to a certain category. `Serializable` and `Cloneable` are examples.
-
Give an example of a situation where you would use an interface.
- Answer: Designing a system with different payment gateways (PayPal, Stripe, etc.). Create a PaymentGateway interface with methods like `processPayment()`. Each gateway implements this interface, allowing the system to handle payments without knowing the specifics of each gateway.
-
What is the difference between a functional interface and a regular interface?
- Answer: A functional interface is an interface that contains exactly one abstract method. It can have multiple default, static, or private methods but only one abstract method. This is crucial for lambda expressions and method references.
-
Provide an example of a functional interface.
- Answer: `java.util.function.Predicate
`, `java.util.function.Consumer `, `java.util.function.Function ` are examples of built-in functional interfaces.
- Answer: `java.util.function.Predicate
-
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.
-
What is the `@FunctionalInterface` annotation?
- Answer: This annotation is used to explicitly mark an interface as a functional interface. It's not mandatory but is good practice to improve code readability and prevent accidental addition of multiple abstract methods.
Thank you for reading our blog post on 'Interface in Java Interview Questions and Answers for freshers'.We hope you found it informative and useful.Stay tuned for more insightful content!