What is interface how you create interface java why we use it

What is interface

Answer: An interface is a group of related methods with empty bodies. Example:

package com.javahonk.interfacetest;

public interface CarInterface {

	public void changeGear(int newValue);

	public void speedUp(int increment);

	public void applyBrakes(int decrement);
}

 

Any class who implements this interface name would change to a particular brand and add their own specification. Also you have to use implements keyword in the class declaration:

package com.javahonk.interfacetest;

public class Toyota  implements CarInterface{

	public static void main(String[] args) {		

	}

	@Override
	public void changeGear(int newValue) {
		// TODO Auto-generated method stub

	}

	@Override
	public void speedUp(int increment) {
		// TODO Auto-generated method stub

	}

	@Override
	public void applyBrakes(int decrement) {
		// TODO Auto-generated method stub

	}

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *