A Java Interface, as first glance, may look similar to a class. However, there are important differences, not only in how it is used, but in defining it from a class that make it worth noting.
Uses of an Interface
Java prevents a developer from extending a Class with two or more classes. This is because if the two parent classes have the same method, it would be difficult to determine which one should be used.
A solution to that was the Java Interface. In many ways, they are similar to an abstract class, however there are differences. Within an interface you can declare as many methods as you wish.
Interfaces allow for a set of known methods to be implemented into a class. For example, you might need to serialize your object, so you implement a serializable interface. This means code is used in serializing data is more consistent and can be implemented through other projects easier. This also holds true for other cases.
It can also be used to ensure that commonly needed functionality is always included, reducing the chances of a developer overlooking a needed class.
Similarities to Java Classes
First, it is a collection of abstract methods. However, it can only have abstract methods.
The file will have a java extension, just like a Class.
An interface can be part of a package, just like a class.
Differences from Java Classes
A Java interface however also has several differences between classes.
First, all methods are abstract. This means that while certain methods must be written, however none are pre-written for you.
An interface cannot have an constructors. This is because it is not a class, and therefore cannot be instantiated, so it doesn’t need a constructor.
An interface cannot contain instance properties. The only properties that can appear in an interface must be declared both static and final.
A class can implement multiple interfaces.
Creating an Interface
Creating the source code for an interface is not difficult. You will need a .java file with the same name as your interface, and then within that file you will declare your interface as necessary.
// File name : NameOfInterface.java
public interface NameOfInterface {
// Any number of final, static fields
// Any number of abstract method declarations
public abstract void sampleMethod();
}
Implementing an Interface
Implementing an interface when you define a class is simple. You simply create a class as normal, and then use the keyword implements
after you define the class name (and if you extend the class from another class) and then give the interface name.
public class SampleClass implements NameOfInterface { ... }
Real World Examples
Let’s look at a real world example. Consider you wanted to model in Java animals. You might have several common attributes like name, height, weight, warmblooded, etc, along with their associated methods. You put those into a class called Animal.
You might then have child classes, such as Reptile, Fish, Bird, and Mammal, which provide additional information. Each of these classes extends
Animal.
However, when you look at locomotion, you run into an issue. For example, you might be tempted to say swim belongs with Fish. However, some Reptiles and some Mammals can also swim, but not all. You might discover something similar for walking and running as even some fish can “walk”, and some birds cannot fly.
This is what can make designing an object oriented solution a challenge as you have to look at all of these particular, possible, combinations and options.
Upon looking further, you realize that having interfaces to define locomotion might be a good idea.
public class Animal { ... }
public class Fish extends Animal implements Swim { ... }
public class Mammal extends Animal { ... }
public class Human extends Mammal implements Swim, Walk { }
Notice here how we have classes that extend other classes, and how they can implement an existing Interface. (Note: The interfaces Swim and Walk are assumed to already exist.) Also notice that it is possible to implement more than one interface, which is one of the strengths of Interfaces.
New Features in JDK 1.8 and greater
Since the beginning of Java, and interface could only provide abstract methods which had to be overridden in the new class that was implementing it. Starting in JDK 1.8, you can define a default method in an interface, as you see below. Notice the keyword default
being used.
interface SampleInterface
{
// set a default method for an interface
default void display()
{
System.out.println("hello");
}
}
class SampleClass implements SampleInterface
{
public static void main (String[] args)
{
SampleClass sco = new SampleClass();
sco.display();
}
}
Java Interfaces was originally found on Access 2 Learn