Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Class vs Interfaces
Next article icon

Java Class vs Interfaces

Last Updated : 11 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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,

  • A class defines the state of behaviour of objects.
  • An interface defines the methods that a class must implement.

Class vs Interface

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 Java

A 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:

  • Modifiers: A class can be public or has default access (Refer to this for details).
  • Class name: The name should begin with an initial letter (capitalized by convention).
  • Superclass(if any): The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent.
  • Interfaces(if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface.
  • Body: The class body surrounded by braces, { }.

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);     } } 

Output
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 Java

An 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).

  • Interfaces specify what a class must do and not how.
  • It includes default and static methods with implementations (from Java 8).
  • If a class implements an interface and does not provide method bodies for all functions specified in the interface, then the class must be declared abstract.
  • A Java library example is Comparator Interface. If a class implements this interface, then it can be used to sort a collection.

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);     } } 

Output
Geek 10 

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".


Next Article
Java Class vs Interfaces

D

deepthisenthil
Improve
Article Tags :
  • Java
  • Difference Between
  • java-interfaces
  • Java-Classes
Practice Tags :
  • Java

Similar Reads

    Java Interface
    An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
    12 min read
    Java List Interface
    The List Interface in Java extends the Collection Interface and is a part of the java.util package. It is used to store the ordered collections of elements. In a Java List, we can organize and manage the data sequentially. Key Features:Maintained the order of elements in which they are added.Allows
    15+ min read
    Java Functional Interfaces
    A functional interface in Java is an interface that contains only one abstract method. Functional interfaces can have multiple default or static methods, but only one abstract method. Runnable, ActionListener, and Comparator are common examples of Java functional interfaces. From Java 8 onwards, lam
    7 min read
    Nested Interface in Java
    In Java, we can declare interfaces as members of a class or another interface. Such an interface is called a member interface or nested interface. Interfaces declared outside any class can have only public and default (package-private) access specifiers. In Java, nested interfaces (interfaces declar
    5 min read
    Java Interface Methods
    In Java, an interface is an abstract type used to specify the behaviour of a class. One of the most important rules when working with interfaces is understanding how methods are declared and implemented. In this article, we are going to learn the interface methods in detail.Note: Interface methods a
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences