Please disable adblock to view this page.

← Go home

Interface in Java


May 2, 2017
Published By : Pratik Kataria
Categorised in:


  • In Object Oriented Programming, an interface is a contract.
  • It defines a set of methods with specific signatures and any class that implements that interface must implement those methods.
  • Example: List and Map

Understanding through Code (You can understand the folder path through package)


import java.util.List;

public interface Details {
	public void getDetails(List<Bike> bikes);  //we are not implementing the method here

	public void setColor(String color);



public class Bike {

	public static final String HONDA = "Honda";
	public static final String YAMAHA = "Yamaha";

	private String name = HONDA;
	private String color = "Red";
	private int gearOrNot = 1;

	public Bike(String name, String color, int gearOrNot) { = name; 
		this.color = color;
		this.gearOrNot = gearOrNot;

	public String getName() { return name; }

	public String getColor() { return color; }

	public int getGearOrNot() { return gearOrNot; }



public class Honda extends Bike { 

	public Honda() {
		super(Bike.HONDA, "Red", 1);



//FOr demonstrating instance method
import java.util.List;

public class VehicleTypes implements Details {

	private String colorChoice;

	public void getDetails(List<Bike> bikes) {  //we must define the method of interface

		String colorChoiceOfCustomer = colorChoice;

		for(Bike b: bikes) {
		 	System.out.println(b.getName() +" " + b.getColor() + " " + b.getGearOrNot());

		System.out.println("Color choice is: " +colorChoiceOfCustomer);


	//now we need to either set setColor method as abstract or define it.
	//abstract just means you are putting the signature of method but not defining it.

	public void setColor(String color) {
		colorChoice = color;



import java.util.List;
import java.util.ArrayList;

public class Vehicle {
	public static void main( String[] args ){

		List<Bike> bikes = new ArrayList<>();

		bikes.add(new Honda());
		bikes.add(new Bike(Bike.YAMAHA, "Black", 1));
		bikes.add(new Bike(Bike.YAMAHA, "Black", 1));

		//Instead of:
		//VehicleTypes vt = new VehicleTypes();
		//We make use of interface:
		Details vt = new VehicleTypes();



  • K:\Interface>javac com\example\java\model\
  • K:\Interface>javac com\example\java\
  • K:\Interface>javac com\example\java\
  • K:\Interface>javac com\example\java\
  • K:\Interface>java
  • Honda Red 1
  • Yamaha Black 1
  • Yamaha Black 1
  • Color choice is: White


  • Instead of setting data-type as VehicleTypes class, we set as Details which implies:
    • VehicleTypes is an instance of Details interface
  • We could call setColor() method since VehicleTypes class by implementing the interface promised to define the method.
  • If you instantiate interface directly then you need to define all the methods yourself.
    • That is: Details vt1 = new Details();

Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.