Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Differences between Interface and class in Java
In Java, both classes and interfaces are fundamental building blocks of object-oriented programming. A class provides a complete blueprint for objects, while an interface defines a contract of behaviors that implementing classes must follow.
Class
A class is a blueprint from which individual objects are created. A class can contain the following variable types −
- Local Variables − Defined inside methods, constructors, or blocks. They are created when the method is called and destroyed when it completes.
- Instance Variables − Declared within a class but outside any method. They are initialized when the class is instantiated and can be accessed from any method of that class.
-
Class Variables − Declared with the
statickeyword, shared across all instances of the class.
Interface
An interface is a reference type in Java, similar to a class. It is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods, an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
A class describes the attributes and behaviors of an object, while an interface defines only the behaviors that a class must implement.
Key Differences
| Feature | Class | Interface |
|---|---|---|
| Methods | Both abstract and concrete methods | Abstract methods (default and static from Java 8) |
| Multiple Inheritance | Not supported | Supported (a class can implement multiple interfaces) |
| Variables | final, non-final, static, non-static | Only public static final
|
| Implementation | Can implement interfaces | Cannot implement, can only extend other interfaces |
| Keyword | class |
interface |
| Inheritance | Extends one class, implements multiple interfaces | Can only extend other interfaces |
| Access Modifiers | private, protected, public, default | Only public members |
| Constructor | Can have constructors | Cannot have constructors |
Example
The following example demonstrates a class implementing an interface ?
public class JavaTester {
public static void main(String args[]) {
Animal tiger = new Tiger();
tiger.eat();
Tiger tiger1 = new Tiger();
tiger1.eat();
}
}
interface Animal {
public void eat();
}
class Tiger implements Animal {
public void eat(){
System.out.println("Tiger eats");
}
}
The output of the above code is ?
Tiger eats Tiger eats
Here, Animal is an interface that defines the eat() behavior. The Tiger class implements this interface and provides the actual method body. The variable tiger is declared as type Animal (interface reference) but holds a Tiger object, demonstrating polymorphism.
Conclusion
A class provides a complete implementation with state and behavior, while an interface defines a contract of methods that implementing classes must fulfill. Use interfaces to achieve multiple inheritance and loose coupling in Java applications.
