Interface in Java
May 2, 2017
Categorised in: Java Core
Interface
- 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
Details.java (You can understand the folder path through package)
package com.example.java;
import java.util.List;
import com.example.java.model.Bike;
public interface Details {
public void getDetails(List<Bike> bikes); //we are not implementing the method here
public void setColor(String color);
}
Bike.java
package com.example.java.model;
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) {
this.name = name;
this.color = color;
this.gearOrNot = gearOrNot;
}
public String getName() { return name; }
public String getColor() { return color; }
public int getGearOrNot() { return gearOrNot; }
}
Honda.java
package com.example.java.model;
public class Honda extends Bike {
public Honda() {
super(Bike.HONDA, "Red", 1);
}
}
VehicleTypes.java
package com.example.java;
//FOr demonstrating instance method
import java.util.List;
import com.example.java.model.Bike;
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;
}
}
Vehicle.java
package com.example.java;
import com.example.java.model.*;
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();
vt.setColor("White");
vt.getDetails(bikes);
}
}
Execution
- K:\Interface>javac com\example\java\model\Bike.java
- K:\Interface>javac com\example\java\Details.java
- K:\Interface>javac com\example\java\VehicleTypes.java
- K:\Interface>javac com\example\java\Vehicle.java
- K:\Interface>java com.example.java.Vehicle
- Honda Red 1
- Yamaha Black 1
- Yamaha Black 1
- Color choice is: White
Notes
- 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.