Java/J2EE and SOA (348 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (16 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (345 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is the difference between Class and Interface?

Published on Oct 07,2019 9K Views


Class and Interface in Java are two of the most important concepts that lay the foundation of Java. But often people get confused about their working. Through the medium of this article, I will be giving you complete insights into the difference between class and interface in Java.

Below are the topics I will be covering in this article:

Class in Java

A class in Java is a blueprint from which an object is created. Every Java class must belong to some packages which are nothing but a group of similar types of classes, interfaces, and sub-packages bundled together. A class is a logical entity that defines the behavior and properties of an object. In other words, a class in Java is used to create and define objects, object data types, and methods. It can only be accessed from outside via its object. Classes as a whole are categories and objects are items within each category. A class declaration generally constitutes of the following parts:

  • Modifiers
  • Class name
  • Keywords
  • The class body within curly brackets {}

A class can be inherited by any number of classes using the extended Below I have shown a skeleton of a class:

modifier class class_name{
/*
fields
...
methods
*/
}

If you wish to learn more about classes, you can refer to our article on Classes in Java. Let’s now move further in this article and learn what is an interface in Java.

Interface in Java

An interface in Java is one of the reference types defined in Java. It is syntactically similar to a class but contains just the method declarations leaving out their implementations. This concept is was introduced to remove the restriction of the Java classes inheriting only one class at a time. To create an interface the keyword interface is used. Along with abstract methods, an interface can also include constants, static methods, nested interfaces and default methods. Any number of classes can implement an interface by using the implements keyword. But you must make sure that the classes implementing an interface are providing the implementation of all the methods declared in that interface. Moreover, just like classes, an interface an also inherit other interfaces using the extend keyword. But then the implementing class needs to provide the implementations of all the methods present in both the interfaces. Also, the methods within an interface must always be declared as public to provide accessibility to the implementing classes. Below I have created a skeleton of an interface:

interface interface_name {

/*
modifier type var_name= value;
modifier type method1(parameter-list);
modifier type method2(parameter-list);
.
.
*/
}

If you wish to learn more about interfaces, you can refer to our article on Interfaces in Java. Let’s now move further in this article and check out the tabular differences between class and interface in Java.

Difference between class and interface in Java

ClassInterface
A class can be instantiatedAn interface can never be instantiated
The class keyword is used to declare itThe interface keyword is used
The members of a class can be declared as private, public or protectedThe members of an interface are always declared as public
Contains the concrete methods i.e methods with bodyContains abstract method i.e methods without the body
The extends keyword is used to inherit a classThe implements keyword is used to use an interface
Can contain final and static methodsCannot contain final or static methods
A Java class can have constructorsAn interface cannot have constructors
A class can extend only one class but can implement any number of interfacesAn interface can extend any number of interfaces but cannot implement any interface

This brings us to the end of this article on the difference between class and interface in Java. I hope I was able to keep the concepts clear and concise. If you want to know more about Java you can refer to our other Java Blogs.

Now that you have understood what are the difference between Class and Interface in Java, check out the Java Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA Training and Certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this “Difference Between Class and Interface” article and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 4th May,2024

4th May

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is the difference between Class and Interface?

edureka.co