Full Stack Web Development Internship Program
- 5k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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:
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.
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.
Class | Interface |
A class can be instantiated | An interface can never be instantiated |
The class keyword is used to declare it | The interface keyword is used |
The members of a class can be declared as private, public or protected | The members of an interface are always declared as public |
Contains the concrete methods i.e methods with body | Contains abstract method i.e methods without the body |
The extends keyword is used to inherit a class | The implements keyword is used to use an interface |
Can contain final and static methods | Cannot contain final or static methods |
A Java class can have constructors | An interface cannot have constructors |
A class can extend only one class but can implement any number of interfaces | An 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.
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co