Classes and interfaces are fundamental components of Java used to define object structure and behavior. A class encapsulates data and methods, while an interface defines a contract that implementing classes must follow.
- A class can extend only one class but implement multiple interfaces.
- An interface can extend multiple interfaces but cannot extend a class.

A class can extend another class and implement an interface, while an interface can only extend another interface, not implement a class.
Class
A class is a blueprint for creating objects. It defines fields to store data, methods to perform operations, and constructors to initialize objects.
class Student{
int id;
String name;
public static void main(String[] args){
Student s = new Student();
System.out.println(s.id);
System.out.println(s.name);
}
}
Output
0 null
Explanation: The Student class contains two instance variables, id and name. Since they are not explicitly initialized, Java assigns their default values, 0 and null, respectively.
Interface
An interface defines a contract that implementing classes must follow. It specifies behavior without maintaining individual object state.
- Fields are implicitly public, static, and final.
- Abstract methods are implicitly public and abstract.
- Default and static methods are supported since Java 8, and private methods since Java 9.
interface TestInterface{
int VALUE = 10;
void display();
}
class TestClass implements TestInterface{
@Override
public void display(){
System.out.println("Geek");
}
}
class Geeks{
public static void main(String[] args){
TestClass t = new TestClass();
t.display();
System.out.println(TestInterface.VALUE);
}
}
Output
Geek 10
Explanation: The TestClass implements TestInterface and provides an implementation of the display() method. The interface constant value is accessed directly using the interface name.
When to Use a Class or Interface?
Use a class when
- Objects need to maintain individual state.
- Constructors are required to initialize objects.
- Related functionality and data need to be encapsulated.
Use an interface when
- Unrelated classes need to follow a common behavioral contract.
- A class needs to support multiple behavioral contracts.
- Implementation details need to be separated from the contract.
Class Vs Interface
Feature | Class | Interface |
|---|---|---|
Keyword | Declared using class. | Declared using interface. |
Instantiation | Concrete classes can be instantiated. | Cannot be instantiated directly. |
Inheritance | Can extend only one class. | Can extend multiple interfaces. |
Implementation | Can implement multiple interfaces using implements. | Cannot implement classes or interfaces. |
Constructors | Can contain constructors. | Cannot contain constructors. |
Methods | Can contain concrete and abstract methods. Abstract methods require an abstract class. | Can contain abstract, default, static, and private methods. |
Access Modifiers | Members can be public, protected, private, or package-private. | Fields are public. Methods can be public or private, depending on their type. |
Variables | Can contain instance and static variables. | All fields are implicitly public, static, and final. |
State Management | Can maintain individual object state. | Cannot declare instance fields. |
Purpose | Defines object structure, state, and behavior. | Defines a common behavioral contract. |