Difference Between Class and Interface
A class is a complete blueprint: it says what data an object holds and exactly how each method works, and you can build objects from it with new. An interface is a contract: it lists what an implementing class must be able to do, but you can't build anything from the contract alone. A class can sign that contract with implements. Beginners sometimes see an interface as "a class without code", but they play different roles, and this lesson lines them up.
Side by Side
| Class | Interface | |
|---|---|---|
| Purpose | Blueprint for objects: state and behavior | Contract: what implementers can do |
Create objects with new | Yes (unless abstract) | No |
| Instance fields | Yes | No; fields are public static final constants |
| Constructors | Yes | No |
| Method bodies | All methods (unless abstract) | Only default, static and private methods |
| Member access | Any modifier | Implicitly public (or private helpers) |
| Inherits from | One class, with extends | Many interfaces, with extends |
| Is inherited by | Subclasses, with extends | Classes with implements, interfaces with extends |
| Keyword | class | interface |
Both in One Example
public class Library {
interface Borrowable {
int MAX_DAYS = 14;
boolean isAvailable();
void borrow(String member);
}
static class Book implements Borrowable {
private final String title;
private String borrower;
Book(String title) { this.title = title; }
public boolean isAvailable() { return borrower == null; }
public void borrow(String member) {
borrower = member;
System.out.println(member + " borrowed " + title
+ " for " + MAX_DAYS + " days");
}
}
public static void main(String[] args) {
Borrowable item = new Book("Java Basics");
System.out.println("Available: " + item.isAvailable());
item.borrow("Srihitha Anamaneni");
System.out.println("Available: " + item.isAvailable());
}
}Available: true
Srihitha Anamaneni borrowed Java Basics for 14 days
Available: falseHow it works
Borrowable(the interface) declares a constant and two methods without bodies. It holds no data about any book.Book(the class) holds the state,titleandborrower, a constructor, and the code for each method.new Book(...)creates the object; the variable uses the interface typeBorrowable, so it could later hold aMagazineor aDvdtoo.
Try it yourself: add a Dvd class that implements Borrowable with a borrowing period of 7 days instead of MAX_DAYS.
Common Mistakes
Trying new Borrowable(). Interfaces can't be instantiated; create an implementing class.
Expecting interface fields to be per object. MAX_DAYS is one shared constant.
Using extends for a class and an interface. A class implements an interface; only interfaces extend interfaces.
Interview Corner
What is the difference between a class and an interface?
A class is a blueprint that defines state through fields and behavior through method implementations, has constructors, and can be instantiated with new unless it's abstract. An interface defines a contract: abstract methods plus constants, default, static and private methods, with no instance state or constructors, and can't be instantiated. A class extends one class but can implement many interfaces; an interface can extend many interfaces.
Practice
- Easy: List three things a class can contain that an interface can't.
- Harder: Create an interface
Rentablewithlong dailyRate()and two classes,CarandBike, that implement it. Print the cost of renting each for 3 days from aRentable[]array.
Key Takeaways
Was this tutorial helpful?