Implementing multiple interfaces, Declaring and Implementing an Interface


Interfaces

An interface is a reference type, similar to a class, which can be declared by using interface keyword. Interfaces can contain only constants, method signatures, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods. Like abstract classes, Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. Interface is a common way to achieve full abstraction in Java.

Implementing multiple interfaces

A Java class can implement multiple interfaces.

public interface NoiseMaker {
	String noise = "Making Noise"; // interface variables are public static final by default
	String makeNoise(); //interface methods are public abstract by default
}
 
public interface FoodEater {
	void eat(Food food);
}
 
public class Cat implements NoiseMaker, FoodEater {
	@Override
	public String makeNoise() {
		return "meow";
	}
 
	@Override
	public void eat(Food food) {
		System.out.println("meows appreciatively");
	}
}

Notice how the Cat class must implement the inherited abstract methods in both the interfaces. Furthermore, notice how a class can practically implement as many interfaces as needed (there is a limit of 65,535 due to JVM Limitation).

NoiseMaker noiseMaker = new Cat(); // Valid
FoodEater foodEater = new Cat(); // Valid
Cat cat = new Cat(); // valid
 
Cat invalid1 = new NoiseMaker(); // Invalid
Cat invalid2 = new FoodEater(); // Invalid

Note:

  • All variables declared in an interface are public static final
  • All methods declared in an interface methods are public abstract (This statement is valid only through Java 7. From Java 8, you are allowed to have methods in an interface, which need not be abstract; such methods are known as default methods)
  • Interfaces cannot be declared as final
  • If more than one interface declares a method that has identical signature, then effectively it is treated as only one method and you cannot distinguish from which interface method is implemented
  • A corresponding InterfaceName.class file would be generated for each interface, upon compilation

Declaring and Implementing an Interface

Declaration of an interface using the interface keyword:

public interface Animal {
	String getSound(); // Interface methods are public by default
}
 
Override Annotation
 
@Override
public String getSound() {
	// Code goes here...
}

This forces the compiler to check that we are overriding and prevents the program from defining a new method or messing up the method signature.

Interfaces are implemented using the implements keyword.

public class Cat implements Animal {
	@Override
	public String getSound() {
		return "meow";
	}
}
 
public class Dog implements Animal {
	@Override
	public String getSound() {
		return "woof";
	}
}

In the example, classes Cat and Dog must define the getSound() method as methods of an interface are inherently abstract (with the exception of default methods).

Using the interfaces

Animal cat = new Cat();
Animal dog = new Dog();
 
System.out.println(cat.getSound()); // prints "meow"
System.out.println(dog.getSound()); // prints "woof"

Extending an interface

An interface can extend another interface via the extends keyword.

public interface BasicResourceService {
	Resource getResource();
}
 
public interface ExtendedResourceService extends BasicResourceService {
	void updateResource(Resource resource);
}

Now a class implementing ExtendedResourceService will need to implement both getResource() and updateResource().

Extending multiple interfaces

Unlike classes, the extends keyword can be used to extend multiple interfaces (Separated by commas) allowing for combinations of interfaces into a new interface

public interface BasicResourceService {
	Resource getResource();
}
 
public interface AlternateResourceService {
	Resource getAlternateResource();
}
 
public interface ExtendedResourceService extends BasicResourceService, AlternateResourceService {
	Resource updateResource(Resource resource);
}

In this case a class implementing ExtendedResourceService will need to implement getResource(), getAlternateResource(), and updateResource().

Basic Programs