Difference between Interface and Class in Java

By Mohit Uniyal|Updated : June 13th, 2022

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

InterfaceClass
Keyword used: InterfaceKeyword 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:

Difference between Java and Core JavaDifference between C and Java
Difference Between Java and JavaScriptDifference Between Constructor and Method in Java
Difference between JDK and JREDifference between Interface and Class in Java

Comments

write a comment

FAQs on Difference between Interface and Class

  • The major difference between Interface and Class in Java is that class will be used to describe the behavior of the objects in the program whereas interface will carry those behaviors of the objects.

  • Interface in Java programming language is a reference type and almost similar to the class. It is used for inheriting various classes at a time. The example of interface is:

    interface interface_name {


    /*

    modifier type var_name= value;

    modifier type method(parameter-list);

    .

    .

    */

    }

  • No, unlike class, interface can not be used to create the object. This is one of the important difference between class and interface. It can carry the behaviours of the object but can not create it.

  • No, in the interface, a constructor can not be contained. The interface can not have it. The class can carry the constructor but an interface can not.

  • As per the multiple inheritance, the difference between class and interface is that interface can support multiple inheritance and class can not support it. The multiple inheritance is the reason for the implementation of interface in a program.

Follow us for latest updates