Site icon LinuxAndUbuntu

Interfaces in Java

Interfaces in Java

Interfaces in Java

We have already learned about the class. A class is a way of defining how an object would look and behave. A class contains variables and methods. Variables define how an object looks and feels. Methods inside a class define how an object behaves.

An interface is a special type of class. We need to understand what makes an interface special. An interface is a way of defining that the object made using this interface will have to have the methods with its implementation declared in the interface. The interfaces do not accept the implementation of the methods. That means when a class implements this interface, that class will have to provide the implementation for all the methods already declared in the interface.

Below is an example of an Interface.

package interfaceDemo.world.interfacePackage;
public interface InterfaceClass {
	
	String Breaths();
	String Eats();
	String Runs();
	String Play();
	
}

Interface Example

You can see in the above example of an interface. It has only the methods but no implementation of those methods. Also, an interface does not contain any variables. Now whichever class implements the above-mentioned interface will have to provide the implementation for these Breaths(), Eats(), Runs() and Play() methods. Let us make a program to understand the Interface.

Interface Program

Interface

package interfaceDemo.world.interfacePackage;
public interface InterfaceClass {
	
	String Breaths();
	String Eats();
	String Play();
	
}

Human Class

package interfaceDemo.world;
import interfaceDemo.world.interfacePackage.InterfaceClass;
public class Human implements InterfaceClass{
	
	private String EatingHabits;
	
	public Human(String eatingHabits) {
		EatingHabits = eatingHabits;
	}
	@Override
	public String Breaths() {
		return "Breathes";
	}
	@Override
	public String Eats() {
		return EatingHabits;
	}
	@Override
	public String Play() {
		return "Plays and Enjoy life";
	}
}

Animal Class

package interfaceDemo.world;
import interfaceDemo.world.interfacePackage.InterfaceClass;
public class Animal implements InterfaceClass{
	
	public String EatingHabits;
	public Animal(String eatingHabits) {
		EatingHabits = eatingHabits;
	}
	@Override
	public String Breaths() {
		return "Breathes";
	}
	@Override
	public String Eats() {
		return EatingHabits;
	}
	@Override
	public String Play() {
		return "Plays and Enjoys life";
	}
}

Main Class

package interfaceDemo;
import interfaceDemo.world.Animal;
import interfaceDemo.world.Human;
import interfaceDemo.world.interfacePackage.InterfaceClass;
public class MainMethod {
	public static void main(String[] args) {
		
		InterfaceClass Ram = new Human("Non-Veg");
		System.out.println(Ram.Breaths());
		System.out.println(Ram.Eats());
		System.out.println(Ram.Play());
		
		System.out.println("*************************************");
		
		Animal Goat = new Animal("Eats Plants");
		System.out.println(Goat.Breaths());
		System.out.println(Goat.Eats());
		System.out.println(Goat.Play());
	}
}
Result of Interface Program

Explanation of the Program

As you can see in the above program. We have created an interface. In that interface, we have declared three methods called Breaths(), Eats(), and Play(). You can see the methods are only declared, and they have no implementation. The Animal and Human classes that we have created in the same program will provide the implementation when we implement this interface in Human and Animal classes.

We see that Human and Animal classes have a string variable called EatingHabits and one constructor to initialize the value of this string variable. If you do not understand what a constructor is, do not worry. We will study it in further articles, but at this point, you understand a constructor is used to set the value of variables. Below is the construction I am talking about.

public Animal(String eatingHabits) {
		EatingHabits = eatingHabits;
	}

We have provided the implementation for the methods Breaths(), Eats(), and Play() in both Human and Animal classes.

In the Main method, we created the object (Goat) of Animal classes and used the object to call the methods inside the Animal class. We out the whole object with its method in System.out.println() method. The System.out.println is used to print a string. We are using System.out.println() because our methods Breaths(), Eats(), and Play() each of them returns a string.

In the case of Human class, you will see I have not created the object of Human class but InterfaceClass. I have done it on purpose to make you understand that we can create a variable of the type of Interface class and use that variable to store the object of a class.

Note – Be informed that a class can implement multiple interfaces by using a comma (,).

public class Human implements InterfaceClass, InterfaceClass2

Once you implement multiple interfaces in a class, that class will have to provide the implementation for all the methods from those multiple interfaces.

Conclusion

Basically, Interfaces make out life easy when we want to streamline the process of creating any application. We don’t need to remember the methods that we need to create inside a class. We need to create an interface and declared all the methods inside it once and for all. Java will force the class to provide the implementation for all the methods inside the interface, and the developer will never forget to create any methods inside the class. I hope this article is helpful. We will discuss the next keyword in our next article. Till then, Good Luck and take care.

Exit mobile version