What is the primary purpose of an interface in OOP?
2025
What is the primary purpose of an interface in OOP?
- A.
To provide a blueprint for creating objects
- B.
To define a set of methods that must be implemented by implementing classes
- C.
To define constructors for classes
- D.
To create static methods
Attempted by 133 students.
Show answer & explanation
Correct answer: B
In Object-Oriented Programming (OOP), an interface is used to define a contract that implementing classes must follow.
An interface contains method declarations without complete implementation. Any class that implements the interface must provide definitions for those methods.
Interfaces help achieve:
Abstraction
Multiple inheritance support
Standardization of behavior
Example:
interface Animal
{
void sound();
}
class Dog implements Animal
{
public void sound()
{
System.out.println("Bark");
}
}Here, the Dog class must implement the sound() method defined in the interface.
Therefore, the main purpose of an interface is to define methods that implementing classes must implement.