Java Class vs Interfaces
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 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 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“.