Class and Interface both are used to create new reference types. A class is a collection of fields and methods that operate on fields. An interface has fully abstract methods i.e. methods with nobody. An interface is syntactically similar to the class but there is a major difference between class and interface that is a class can be instantiated, but an interface can never be instantiated.
So let us learn some more difference between a class and interface with the help of a comparison chart shown below.
Content: Class Vs Interface
Comparison Chart
Basis for Comparison | Class | Interface |
---|---|---|
Basic | A class is instantiated to create objects. | An interface can never be instantiated as the methods are unable to perform any action on invoking. |
Keyword | class | interface |
Access specifier | The members of a class can be private, public or protected. | The members of an interface are always public. |
Methods | The methods of a class are defined to perform a specific action. | The methods in an interface are purely abstract. |
Implement/Extend | A class can implement any number of interface and can extend only one class. | An interface can extend multiple interfaces but can not implement any interface. |
Constructor | A class can have constructors to initialize the variables. | An interface can never have a constructor as there is hardly any variable to initialize. |
Definition of Class
A class is a most important aspect of Java programming without classes you can’t implement a java program. A class creates reference types and these reference types are used to create objects. A class has a signature and a body. Signature of a class has class’s name and information that tells whether the class has inherited another class. The body of a class has fields and methods that operate on those fields. A Class is created using a keyword class. Lets us see how to declare a class.
class class_name{ /* fields ... methods */ }
When a class is instantiated each object created contains a copy of fields and methods with them. The fields and members declared inside a class can be static or nonstatic. Static members value is constant for each object whereas, the non-static members are initialized by each object differently according to its requirement.
Members of a class have access specifiers that decide the visibility and accessibility of the members to the user or to the subclasses. The access specifiers are public, private and protected. A class can be inherited by an another class using the access specifier which will decide the visibility of members of a superclass (inherited class) in a subclass (inheriting class). Thus class fulfills the concept of data hiding and encapsulation in Object Oriented programming.
Definition of Interface
An interface is also among the reference types defined in Java. The importance of an interface is that in Java, a class can only inherit a single class. To avoid this restriction, the designers of Java introduced a concept of interface. An interface is syntactically similar to a class, but it lacks in field declaration and the methods inside an interface do not have any implementation. An interface is declared using a keyword interface. Let us view the declaration of an interface.
interface interface_name { type var_name= value; type method1(parameter-list); type method2(parameter-list); . . }
An interface does not define any method declared inside it because it lacks in fields to operate on. Just because any method inside an interface does not perform any action, an interface can never be instantiated. If an interface has any field member, it must be initialized at the time of their declaration. An interface never contains any constructors as it lacks in field members to get initialize. So, an interface only defines what a class must do instead of how it must do.
An interface once created can be implemented by any number of classes using a keyword implements. But the classes implementing an interface must define all the methods inside an interface. An interface can also inherit another interface using extend keyword. If a class implements an interface that extends another interface. Then a class must define the methods of both the interfaces appearing in a chain. The methods inside an interface must always be public as they have to be accessed by the classes implementing them.
Key Differences Between Class and Interface in Java
- A class can be instantiated by creating its objects. An interface is never instantiated as the methods declared inside an interface are abstract and does not perform any action, so there is no use of instantiating any interface.
- A class is declared using a keyword class. In the same way, an interface is created using a keyword interface.
- The members of a class can have the access specifier like public, private, protected. But the members of an interface are always public as they have to be accessed by the classes implementing them.
- The methods inside a class are defined to perform an action on the fields declared in the class. As interface lacks in the declaration of fields, the methods inside an interface are purely abstract.
- A class can implement any number of interfaces but can extend only one super class. An interface can extend any number of interfaces but cannot implement any interface.
- A class has constructors defined inside it to get the variable initialized. But, an interface does not have any constructors as there are no fields to be initialized. The fields of an interface are initialized at the time of their declaration only.
Conclusion
Both classes and interfaces have their own importance. A class is used when we need to define how the task would be done. An interface is used when we need to know what task has to be done.
MONOJ MONDAL says
Great explanation.
Sonam says
Fantastic explanation done b/w these two…like it …helped a lot during examination…
Nanetta says
Well explained