Master java skills

Interfaces in java

Interface

Interface is an abstraction of a class. It is still a template for a class but it only tells about the behaviour without its actual implementation. Interface defers the actual implementation to the classes that would implement it. It only defines a type for the classes. It doesn’t have variables. Only constants can be there.

Let’s take one example. We can define an interface called LivingBeing. Now, LivingBeing interface will have abstract behaviour like eat, walk, sit etc. But the concrete behaviour will be defined by the classes that implement LivingBeing interface.

public interface LivingBeing {
	
	public void eat();
	public void walk();
	public void sit();
}

Following points helps us understand interfaces in a better way.

  • An interface is an abstraction of a class. That means it tells what a class must do but not how.
  • Interfaces can only have abstract methods and constants. (However, from java 1.8 onwards, interfaces can also have default and static methods)
  • Interface defines ‘Type’ of a class. A real world example would be a machine which is Repairable. Here ‘Repairable’ is the type of the machine. So, ‘Repairable’ is the interface. Similarly, a disease can be ‘Curable’. So, ‘Curable’ defines the type and hence is an interface.
  • Interface cannot have variables and that makes sense also. Because we don’t create objects from an interface. If we can’t create objects, there is no need to maintain state, and thus, no need to keep variables.
  • Multiple inheritance can be achieved by the use of interfaces.

How to define an interface

syntax:

interface <interface-name> {

//constants
//abstract methods

}

Interface example one:

In this example, we will create one LivingBeing interface and two classes Human and Animal that implement this interface. Both the classes will have different implementations of abstract methods of LivingBeing interface.

public interface LivingBeing {
	
	void eat();
	void walk();
	void communicate();

}
public class Human implements LivingBeing {

	@Override
	public void eat() {
		System.out.println("Humans eat using hands.");
		
	}

	@Override
	public void walk() {
		System.out.println("Humans walk on two legs.");
		
	}

	@Override
	public void communicate() {
		System.out.println("Humans communicate using language.");
		
	}

}
public class Animal implements LivingBeing {

	@Override
	public void eat() {
		System.out.println("Animals eat without using hands.");

	}

	@Override
	public void walk() {
		System.out.println("Animals walk on four legs.");

	}

	@Override
	public void communicate() {
		System.out.println("Animals communicate using gestures and emotions.");

	}

}
public class TestClass {
	
	public static void main(String[] args) {
		
		Human h = new Human();
		h.eat();
		h.walk();
		h.communicate();
		
		Animal a = new Animal();
		a.eat();
		a.walk();
		a.communicate();
		
	}
}
Output :
Humans eat using hands.
Humans walk on two legs.
Humans communicate using language.
Animals eat without using hands.
Animals walk on four legs.
Animals communicate using gestures and emotions.

Interface example two

public interface Repairable {
	
	public int SERVICE_CHARGE = 100;
	
	public int repair();
}
public class Car implements Repairable {
		
	public int repair() {
		System.out.println("The car has been repaired successfully.");
		
		int repairingCharges = 500;
		
		int totalCharges = repairingCharges + SERVICE_CHARGE;
		
		return totalCharges;
	}
	
	public static void main(String[] args) {
		
		Car c1 = new Car();
		
		int totalCharges = c1.repair();
		
		System.out.println("Total Charges = " + totalCharges + ". Please pay and take your car home. " );
	}
}

Output :

The car has been repaired successfully.
Total Charges = 600. Please pay and take your car home.
public class Toy implements Repairable {
	
	public int repair() {
		System.out.println("The toy has been repaired.");
		
		int totalCharges = 100 + SERVICE_CHARGE;
		
		return totalCharges;
	}
	
	public static void main(String[] args) {
		
		Toy t1 = new Toy();
		int totalCharges = t1.repair();
		
		System.out.println("Total Charges = " + totalCharges + ". Please pay and take your toy home.");
	}
}

Output :

The toy has been repaired.
Total Charges = 200. Please pay and take your toy home.