Differences between Interface and Class in Java - GeeksforGeeks

13 Jul, 2021
This article highlights the differences between a class and an interface in Java. They seem syntactically similar, both containing methods and variables, but they are different in many aspects.Class: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 these components, in order:  
Modifiers : A class can be public or has default access (Refer this for details).Class name: The name should begin with a 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, { }.
Modifiers : A class can be public or has default access (Refer this for details).
Class name: The name should begin with a 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 provides the state of the class and its objects, and methods are used to implement the behavior of the class and its objects.Example:  
Java
// Java program to demonstrate Class // Class Declarationpublic class Dog {     // Instance Variables    String name;    String breed;    int age;    String color;     // Constructor Declaration of Class    public Dog(String name, String breed,               int age, String color)    {        this.name = name;        this.breed = breed;        this.age = age;        this.color = color;    }     // method 1    public String getName()    {        return name;    }     // method 2    public String getBreed()    {        return breed;    }     // method 3    public int getAge()    {        return age;    }     // method 4    public String getColor()    {        return color;    }     @Override    public String toString()    {        return ("Hi my name is "                + this.getName()                + ".\nMy breed, age and color are "                + this.getBreed() + ", "                + this.getAge() + ", "                + this.getColor());    }     public static void main(String[] args)    {        Dog tuffy = new Dog("tuffy", "papillon",                            5, "white");        System.out.println(tuffy.toString());    }}
Hi my name is tuffy.
My breed, age and color are papillon, 5, white
 
Interface: Like a class, an interface can have methods and variables, but the methods declared in interface are by default abstract (only method signature, no body).    
Interfaces specify what a class must do and not how. It is the blueprint of the class.
An Interface is about capabilities like a Player may be an interface and any class implementing Player must be able to (or must implement) move(). So it specifies a set of methods that the class has to implement.
If a class implements an interface and does not provide method bodies for all functions specified in the interface, then 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.
Syntax :  
interface <interface_name> {
    
    // declare constant fields
    // declare methods that abstract 
    // by default.
}
Example:  
Java
// Java program to demonstrate// working of interface. import java.io.*; // A simple interfaceinterface in1 {     // public, static and final    final int a = 10;     // public and abstract    void display();} // A class that implements the interface.class testClass implements in1 {     // Implementing the capabilities of    // interface.    public void display()    {        System.out.println("Geek");    }     // Driver Code    public static void main(String[] args)    {        testClass t = new testClass();        t.display();        System.out.println(a);    }}
Geek
10
 
Differences between a Class and an Interface:
 
 
saurabh1990aror
Java-Classes
java-interfaces
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference between Process and Thread
Stack vs Heap Memory Allocation
Difference Between Method Overloading and Method Overriding in Java
Differences between JDK, JRE and JVM
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java