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.
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:
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"
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().
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions