Master java skills

Interfaces in Java

Interfaces are a way of achieving abstraction in Java. It is blueprint of a class. Following points are important when we talk about interfaces.

  1. An interface is a blueprint of a class
  2. We can also understand it as a complete abstraction of a class
  3. Before java 8, interfaces could only have abstract methods, but after java 8 interfaces can also have concrete methods which are known as Default and static methods.
  4. Interfaces cannot have variables. They can only have constants
  5. private and protected access modifiers are not allowed for abstract methods in interfaces.

IS-A relationship

Interface defines a type for an object. So, objects of a class represent IS-A relationship with the interface it implements.

Let’s understand this with an example. Suppose, we have a set of toys. Some of them are repairable and some are unrepairable. Let’s create one interface called Repairable.

package com.javatrainingschool;

public interface Repairable {

	void repair();
	
}

Now, Toy class implements Repairable interface. Thus, we can say – Toy IS-A Repairable type.

package com.javatrainingschool;

public class Toy implements Repairable{

	public void repair() {
		System.out.println("Toy repaired.");
	}
	
}

Let’s consider below example. We have a class called ‘Man’. And it implements ‘Human’ interface. We have an object of ‘Man’ class called Rancho. So, we can say Rancho IS-A Human.

public class Man implements Human{
}
Man rancho = new Man();

Defining interfaces

Below are some examples of interface. Only abstract methods and constants.

public interface Animal {
       
       public final int NO_OF_EYES = 2;
       public void eat();
       public void walk();
}

public interface Human {

       public final int NO_OF_LEGS = 2;
       public void read(String text);
       public void showEmotions();
}

Advantages of using interfaces

How classes implement interfaces

Classes can implement interfaces using implements keyword. There are certain rules of doing this.

  1. One class can implement multiple interfaces.
  2. A class must provide implementations to all abstract methods from its implemented interfaces
  3. If a class choose not to provide implementation to one or more methods, then the class must be declared as abstract.

Let’s see one example. Here we are going to create two interfaces, LivingBeing and Human. Then class TennisPlayer will implement both of these interfaces.

package com.javatrainingschool;

public interface LivingBeing {
	
	void eat();
	void breathe();

}
package com.javatrainingschool;

public interface Human {
	
	final int NO_OF_EYES = 2;
	
	void read(String text);

}
package com.javatrainingschool;

public class TennisPlayer implements LivingBeing, Human {
	
	private String name;
	
	public TennisPlayer(String name) {
		super();
		this.name = name;
	}

	@Override
	public void read(String text) {
		System.out.println(this.name + " reads " + text);
	}

	@Override
	public void eat() {
		System.out.println(this.name +  " eats food.");
		
	}

	@Override
	public void breathe() {
		System.out.println(this.name + " breathes.");
		
	}
	
	public void playTennis() {
		System.out.println(this.name + " plays Tennis");
	}
	
	public static void main(String[] args) {
		
		TennisPlayer p1 = new TennisPlayer("Roger Federer");
		p1.breathe();
		p1.eat();
		p1.read("novel");
		p1.playTennis();
		
	}

}
Output :

Roger Federer breathes.
Roger Federer eats food.
Roger Federer reads novel
Roger Federer plays Tennis

Multiple Inheritance using interfaces

As we know due to diamond problem, multiple inheritance using classes is not allowed in Java. However, an interface can still extends multiple interfaces because it doesn’t cause any ambiguity.

public interface LivingBeing {
       void breathe();
}

public interface Creature {
       void eat();
}

public interface Animal {
       void sleep();
}

public interface Mamamls extends LivingBeing, Creature, Animal {
       void feedChildrenMilk();
}

One class cannot extend more than one class but they can implement multiple interfaces. We have already seen the example above where TennisPlayer class implements two interfaces : LivingBeing and Human.

Inheritance of interfaces

One interface can extend multiple interfaces. We have already seen it in the above example, where Mamals interface is extending LivingBeing, Creature and Animal interfaces.

In this way, all the abstract methods of parent interfaces are inherited by the child interface.

Java 8 Default method

Java 8 default method have been discussed in detail here.

Java 8 Static method

Java 8 static method have been discussed in detail here.

Inner or nested interfaces

An interface defined within another interface is called an inner or nested interface. Let’s see an example

public interface ABC {

      final String CONSTANT_VALUE = "abc";
      void show();
      
      // XYZ is an inner interface
      interface XYZ {
          void display();
      }
}