Interface in Java is the core concept every developer must know about. It explains the design and structure of an application by defining the abstract type to specify the behavior of a class. Do you know how? This blog here explains everything around what is interface in Java, including its definition, properties, relation with class and more. Let's begin with the definition:
An interface in Java is a group of abstract methods used to achieve a unique abstraction in object oriented programming. This abstraction involves defining methods even without actual implementations. Think of it as a reference type that is similar to the class. In some instances, interfaces also contain static methods, default methods, constants and nested types. One crucial fact is that method bodies only exist for default and static methods.
Now you might be wondering how to write an interface. It is very similar to writing a class. The difference is that a class always defines the behaviors and attributes of an object, whereas interface defines the behaviors implemented by the class. Therefore, if the class implementing the interface is not abstract, one may have to define all the methods of the interface.
With the last statement, you might be confused about when to use an interface in Java. In this section, we will understand its perfect use. Consider the following points to decide between interface and class:
Understanding what is interface in Java also involves knowing its properties. It helps in making good use of the Java interface. Consider the following:
Interfaces offer many advantages in Java programming, especially when building scalable and flexible applications. They help developers create a proper structure for classes and improve code maintainability. By using interfaces, developers can achieve abstraction, loose coupling and multiple inheritance in a clean and efficient way. Below are some major advantages of interfaces in Java:
Still confused between interface and class or having some doubts? This table will help you to gain a good understanding.
| Feature | Class | Interface |
| Purpose | Blueprint for objects that define state and behavior. | A behavior contract that defines what a class should do. |
| Implementation | Provides concrete implementation for methods. | Before Java 8: No method implementation. Java 8+: Default and static methods can have implementation. |
| State | Can have instance variables. | Cannot have instance variables. |
| Constructors | Can have constructors. | Cannot have constructors. |
| Inheritance | Extends one class (single inheritance). | Implements multiple interfaces (multiple inheritance of type) |
| Access | Members can have various access modifiers. | All methods are implicitly public and abstract (pre-Java 8). Fields are public static finals. |
| Keywords | class, extends, implements | interface, implements |
| Instantiation | Can be instantiated (unless abstract). | Cannot be instantiated directly. |
| Abstract Methods | Can have both concrete and abstract methods. If it has an abstract method, it must be declared abstract. | All methods are implicitly abstract (pre-Java 8). Can have default, static, and private methods (Java 8+). |
Let's undersand Java interface with the help of some real-world examples:
Let's take a real-world example of using an interface to understand how to use it. Here is an instance showing how to define a contract that a class can fulfill.
|
Mammal eats Mammal travels |
Here, the Animal interface defines a contract: eat() and travel() methods must be implemented. The MammalInt class implements this contract, providing its unique logic for "Mammal eats" and "Mammal travels." It also adds its own noOfLegs() method. This demonstrates how interfaces enforce behavior while allowing diverse class implementations, ensuring consistency and promoting polymorphism.
This example shows how an interface can define a common behavior that a class must implement.
|
I am a student |
Here, the Person interface contains the introduce() method. The Student class implements the interface and provides its own implementation. This shows how an interface defines behavior while the implementing class decides how that behavior works.
This example demonstrates how different classes can follow a common contract defined by an interface.
|
Drawing Circle |
The Shape interface declares the draw() method. The Circle class implements the interface and provides the actual implementation. This allows different shape classes to follow the same structure while performing different actions.
One of the biggest advantages of interfaces is that they allow multiple inheritance in Java, which is not possible with classes. You might already know that a class can extend only one parent class due to single inheritance limitations. But what if you need a class to inherit behavior from multiple sources? This is where interfaces shine.
A single class can implement multiple interfaces at the same time. This means your class can follow multiple contracts without any conflict. Let’s understand this with a simple example:
|
Duck is flying Duck is swimming |
Here, the Duck class implements both Flyable and Swimmable interfaces. It provides implementations for both fly() and swim() methods. This shows how interfaces enable multiple inheritance of type, allowing a class to exhibit behaviors from different contracts. This is a clean and safe way to achieve code reusability and flexibility.
Since Java 8, interfaces are no longer just abstract contracts. Oracle introduced powerful new features that make interfaces more functional and practical. Let’s explore the key additions:
Now you can provide a default implementation for methods in an interface. This helps in evolving interfaces without breaking existing code.
|
Car started Beep beep! |
Interfaces can now have static methods with implementation. These are utility methods related to the interface.
|
Sum: 8 |
You can now define private methods inside interfaces to reuse code between default methods.
|
These features make interfaces more powerful and reduce boilerplate code. They allow backward compatibility, code sharing, and better design patterns like the Strategy or Template Method.
This article has explained why, where and how to use interfaces in Java. It will help you to build a new skill to become a Java developer. To gain more knowledge and build better skills, refer to our Java tutorial for beginners. It will help you to understand all the essential concepts of this programming language.
Related Articles:
Interfaces are used for many purposes, including abstraction, multiple inheritance, polymorphism and loose coupling.
These rules define a contract for classes to follow. They establish a set of methods that any class implementing the interface must provide. This ensures consistent behavior across different classes and promotes abstraction and loose coupling.
You should ensure that names should be capitalized like class names, following the UpperCamelCase convention. For example, Runnable, Serializable, List.
An abstract class can have both abstract and non-abstract methods, while an interface defines only method declarations and is used to achieve multiple inheritance.
A class can contain method implementations. An interface only declares methods without implementations.