Difference between Interface and Class in Java
By BYJU'S Exam Prep
Updated on: September 25th, 2023
Difference between Interface and Class in Java: In Java programming language, both class, and interface are used to lay the foundation of the language. Thes both are related to the behaviours of the object described in the content. The main difference between interface and class in Java is that class describes the behaviour of the object and interface contains those behaviours which were assigned and defined by the class.
Both class and interface are an essential part of the program. Here, we will discuss what is class and interface in Java and along with it we will discuss in detail the difference between class and interface in various aspects in the coming sections.
Table of content
Difference Between Interface and Class in Java
Class and Interface in Java programming language are really important aspects and are similar to each other. But there exist a few differences between the two which are elaborated in the table provided below on various aspects such as inheritance, variables, Methods, etc.
Key Differences Between Interface and Class in Java
Interface | Class |
Keyword used: Interface | Keyword used: class |
Objects can not be created. | Objects of the class can be created. |
Multiple inheritances are supported. | Multiple inheritances are not supported. |
Can not inherit a class. | Can inherit the class. |
Only abstract methods are contained. | Constructors are contained sometimes. |
Variable can be static or final. | Variables can be static, final or neither. |
Methods and variables are public. | Methods and variables are not declared public. |
What is Class in Java?
In Java programming language, in order to create the object, a user-defined prototype is required. This user-defined prototype is known as the class of the program. This prototype carries all the required and essential properties of objects of one type.
The state of a class in Java is provided by the fields which are known as the variable in the programming language. There are various components mentioned in the class. The components are listed below:
- Modifiers
- Class name
- Superclass or subclass
- Interfaces
- Body
Example of class:
modifier
class
class_name{
/*
fields
...
methods
*/
}
What is Interface in Java?
The interface in Java programming language is very much similar to the class but only carries method declarations. It is a reference type. The syntax of the interface is similar to that of the class. The interface in Java programming language is used for inheriting more than one class at a time.
Interface Using an extended keyword, the interface can be inherited. It is used to carry the behaviour assigned to the object and does not carry the methods of following those behaviours. The interface of the Java can be written as:
interface interface_name {
/*
modifier type var_name= value;
modifier type method(parameter-list);
.
.
*/
}
Check out some important topics related to the Difference Between Interface and Class in Java: