In the previous tutorial, we have explained the difference between abstract class and interface, which is one of the most popular questions in an interview.
Now let’s understand the difference and comparison between class and interface in Java.
In fact, an interface is declared using syntax very similar to that of a class definition, but there is also a major difference between class and interface in Java.
The first major difference is that a class can be instantiated, but an interface can never be instantiated.
Second is that methods in an interface are only declared but not implemented, i.e. methods do not have a body. Let’s see more differences between them one by one.
Java Class vs Interface
a. A class is declared by using a keyword called class.
b. An interface is declared by using a keyword interface.
2. Instantiate:
a. A class can be instantiated. The keyword new can only create an instance of a class.
b. An interface can never be instantiated. That is, an object of interface can never be created.
3. Variables:
a. Variables in a class can be declared using any access modifiers, such as public, protected, default, and private. They can also be static, final, or neither.
b. All variables in an interface are always public, static, and final.
4. Methods:
a. Methods declared in a class are implemented. i.e. methods have a body. Methods that have a body is called concrete method.
b. Methods declared in an interface cannot be implemented. i.e. In an interface, methods have no body. It contains only abstract method.
Note: Java 8 introduces a default method, which allows us to give the body of a method in an interface.
For example:
public interface DefaultDemo { void m1(); default void m2() { . . . . . . . . } }
5. Access modifiers:
a. The members of a class can be declared with any access modifiers, such as private, default, protected, and public.
b. The members of an interface are always public by default.
6. Constructors:
a. A class can have constructors to initialize instance variables.
b. An interface cannot have any constructors.
7. Inheritance:
a. Class allows only multilevel and hierarchical inheritances but does not support multiple inheritance.
b. Interface supports all types of inheritance, such as multilevel, hierarchical, and multiple inheritance.
8. Implement:
a. A class can implement any number of the interface.
b. Interface cannot implement any class.
9. Extends:
a. A class can extend only one class at a time.
b. An interface can extend multiple interfaces at a time.
10. Use:
a. Interface is used for defining behavior that can be implemented by any class anywhere in the class hierarchy.
b. A class is used to define the attributes and behaviors of an object.
11. Final:
a. The method in an interface cannot be final because if you declare a method as final in interface, the method cannot be modified by any of its subclasses.
b. A method in a class can be declared as final.
12. Main method:
a. A class may contain main() method.
b. Interface cannot have main() method.
Java Class Example Program
Let’s take an example program where we will deal with all the above important points related to the class.
Program code:
package classProgram; public class ClassTest { public int a = 20; int b = 30; protected int c = 30; private int d = 40; void m1() { // Declaration of method with default modifier. System.out.println("Methods have a body"); } // Declaration of constructor in class. ClassTest() { System.out.println("Constructor in class"); } } public class Myclass extends ClassTest { private void m2() { System.out.println("Single level inheritance"); } public static void main(String[] args) { Myclass mc = new Myclass(); System.out.println("Value of a: " +mc.a); System.out.println("Value of b: " +mc.b); System.out.println("Value of c: " +mc.c); mc.m1(); mc.m2(); } }
Output: Constructor in class Value of a: 20 Value of b: 30 Value of c: 30 Methods have a body Single level inheritance
Java Interface Example Program
Let’s take an example program where we will see all the above points related to the interface.
Program code:
package interfacePrograms; public interface AA { int x = 20; void m1(); } public interface BB { int y = 30; void m2(); } public class CC implements AA, BB { public void m1() { System.out.println("Value of x: " +x); } public void m2() { System.out.println("Value of y: " +y); } public static void main(String[] args) { CC c = new CC(); c.m1(); c.m2(); } }
Output: Value of x: 20 Value of y: 30
In this tutorial, we have explained all important points related to the difference between class and interface in Java. Hope that you have understood all the points of class vs interface. In the next, we will understand the default method Java 8 interface.
Thanks for reading!!!