In Java, the differences between class and interface are syntactically similar but different in various aspects. Both the class and interface have methods, variables, and constants. In this section, we will discuss the differences between class and interface.
A class is a blueprint for creating objects. It contains variables and methods that define the properties and behaviour of objects.
Here is an example of a class in Java.
Here, Animal is a class with a variable (name) and a method (makeSound).
There are several advantages of classes in Java. Some of them are as follows:
An interface is like a contract that specifies a set of methods that a class must implement. It does not contain any actual code for methods (except in special cases).
Here is an example of an interface in Java.
Here, Animal is an interface that only declares a method but does not define how it works.
There are several advantages of Interfaces in Java. Some of them are as follows:
You can use the class or interfaces in the following cases:
The following table shows the key differences between class and interface in Java based on the different aspects:
| Feature | Class | Interface |
|---|---|---|
| Methods | It can have both normal and abstract methods. | It can only have abstract methods (before Java 8). Default and static methods are allowed from Java 8. |
| Variables | It Can have any type of variable. | It Can only have final and static variables. |
| Inheritance | A class can extend another class (single inheritance). | An interface can extend multiple interfaces (multiple inheritance). |
| Implementation | A class can implement an interface using implements. | An interface cannot implement another interface, only extend it. |
| Constructors | It can have constructors. | It cannot have constructors. |
| 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. |
| Multiple Inheritance | Not supported for classes. | Supported using multiple interfaces. |
| Access Modifiers | It can have any access modifiers (public, private, protected, default). | Methods are public by default. Private methods allowed from Java 9. |
| Usage | It is used to define the state and behaviour of objects. | It is used to define a contract that multiple classes follow. |
Consider the following example to understand the difference between a class and an interface. In this example, Animal is an interface that defines a common behavior, while Dog and Cat are classes that implement the interface and provide their own implementations.
Output:
Dog barks Cat meows
Explanation
In this example, the Animal interface sets a rule that any class implementing it must define the makeSound() method. The Dog and Cat classes follow the rule by providing their versions: one prints "Dog barks," and the other prints "Cat meows." In the Main class, objects of Dog and Cat are created and assigned to the Animal type. When makeSound() is called, Java dynamically picks the correct method for each object.
Both classes and interfaces are essential in Java, but they serve different purposes. Classes define real-world objects, while interfaces establish a behaviour contract that multiple classes can implement. Understanding their differences helps in designing robust and scalable applications.
We request you to subscribe our newsletter for upcoming updates.