Last Updated : 11 Jul, 2025
In Java, the difference between a class and an interface is syntactically similar; both contain methods and variables, but they are different in many aspects. The main difference is,
The following table lists all the major differences between an interface and a class in Java.
Features
Class
Interface
Keyword
The keyword used to create a class is "class".
The keyword used to create an interface is "interface".
Instantiation
A class can be instantiated, i.e., objects of a class can be created.
An interface cannot be instantiated directly, instead, it is implemented by a class or a struct.
Inheritance
Classes do not support multiple inheritance.
Interface supports multiple inheritance.
Inheritance Mechanism
A class can inherit another class using the keyword extends.
A class can implement an interface using the keyword "implements". An interface can inherit another interface using "extends".
Constructors
It can contain constructors.
It cannot contain constructors.
Methods
Methods in a class can be abstract, concrete, or both.
An interface contains abstract methods by default (before Java 8) or default/static methods (from Java 8 onward).
Access Specifiers
Variables and methods in a class can be declared using any access specifier(public, private, default, protected).
All variables and methods in an interface are declared public
Variables
Variables in a class can be static, final, or neither.
All variables are static and final.
Purpose
A class is a blueprint for creating objects and encapsulates data and behaviour.
An interface specifies a contract for classes to implement by focusing on capabilities rather than implementation.
Classes in JavaA Class is a user-defined blueprint or prototype from which objects are created. It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include the following components:
Constructors are used for initializing new objects. Fields are variables that provide the state of the class and its objects, and methods are used to implement the behaviour of the class and its objects.
Example of a Class:
Java
// Java program to demonstrate class functionality
class Dog {
private String n; // name
private String b; // breed
private int a; // age
private String c; // color
// Constructor to initialize the object
public Dog(String n, String b, int a, String c) {
this.n = n;
this.b = b;
this.a = a;
this.c = c;
}
// Getter methods
public String getN() {
return n;
}
public String getB() {
return b;
}
public int getA() {
return a;
}
public String getC() {
return c;
}
// Override toString() to provide a
// custom string representation
@Override
public String toString() {
return "Name is: " + n +
"\nBreed, age, and color are: "
+ b + ", " + a + ", " + c + "";
}
// Main method
public static void main(String[] args) {
Dog d = new Dog("Tuffy", "Papillon", 5, "White");
System.out.println(d);
}
}
Name is: Tuffy Breed, age, and color are: Papillon, 5, White
Explanation: The above example defines a "Dog" class with many fields for name, age, breed, and color. Then initialized with the constructor and getter method to provide access to the fields. Then to customize the string representation of an object, override the toString() method and the main method creates a Dog object and prints its details.
Interface in JavaAn interface in Java is a blueprint of a class. The interface can have methods and variables, but the methods declared in the interface are by default abstract (only method signature, nobody).
Example of an Interface:
Java
// Java program to demonstrate
// working of interface
import java.io.*;
interface in1 {
final int a = 10;
// public and abstract
void display();
}
// Class that implements the interface
class testClass implements in1 {
// Implementing the capabilities of
// interface
public void display() {
System.out.println("Geek");
}
public static void main(String[] args)
{
testClass t = new testClass();
t.display();
System.out.println(a);
}
}
Explanation: The above example shows how the "testClass" implements an interface "in1" by overriding its abstract method "display()". Then the main method creates an instance of "testClass" and calls the "display()" method then prints the interface constant variable "a".
Differences between Interface and Class in Java
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4