Differences between a Class and an Interface
The following table lists all the major differences between an interface and a class in Java language:
Class |
Interface |
---|---|
The keyword used to create a class is “class” | The keyword used to create an interface is “interface” |
A class can be instantiated i.e., objects of a class can be created. | An Interface cannot be instantiated i.e. objects cannot be created. |
Classes do not support multiple inheritance. | The interface supports multiple inheritance. |
It can be inherited from another class. | It cannot inherit a class. |
It can be inherited by another class using the keyword ‘extends’. | It can be inherited by a class by using the keyword ‘implements’ and it can be inherited by an interface using the keyword ‘extends’. |
It can contain constructors. | It cannot contain constructors. |
It cannot contain abstract methods. | It contains abstract methods only. |
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 as public. |
Variables in a class can be static, final, or neither. | All variables are static and final. |
Differences between Interface and Class in Java
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.