Difference Between Class and Interface

Naveen AnamaneniUpdated · 4 min read

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

ClassInterface
PurposeBlueprint for objects: state and behaviorContract: what implementers can do
Create objects with newYes (unless abstract)No
Instance fieldsYesNo; fields are public static final constants
ConstructorsYesNo
Method bodiesAll methods (unless abstract)Only default, static and private methods
Member accessAny modifierImplicitly public (or private helpers)
Inherits fromOne class, with extendsMany interfaces, with extends
Is inherited bySubclasses, with extendsClasses with implements, interfaces with extends
Keywordclassinterface

Both in One Example

Library.java▶ Try it
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());
    }
}
Output
Available: true
Srihitha Anamaneni borrowed Java Basics for 14 days
Available: false

How it works

  1. Borrowable (the interface) declares a constant and two methods without bodies. It holds no data about any book.
  2. Book (the class) holds the state, title and borrower, a constructor, and the code for each method.
  3. new Book(...) creates the object; the variable uses the interface type Borrowable, so it could later hold a Magazine or a Dvd too.

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

  1. Easy: List three things a class can contain that an interface can't.
  2. Harder: Create an interface Rentable with long dailyRate() and two classes, Car and Bike, that implement it. Print the cost of renting each for 3 days from a Rentable[] array.

Quick check: Class vs interface

4 questions
1Which can be instantiated with new?
2Which can hold per-object state?
3How does a class use an interface?
4Which can have a constructor?

Key Takeaways

Was this tutorial helpful?

Lesson 140 of 482