info@tutsfinder.in



Interface

Through Interface mechanism we can achieve abstraction.

Interface contains only undefined methods i.e. abstract methods(methods without body). A class can implement interface through implements keyword.

Interfaces cannot be instantiated, but can be implemented.

The methods we declare in interface, it is necessary to define them in the class in which we are implementing them otherwise compiler gives an error.




Syntax:

Interface abc { // only methods } Class xyz implements abc { //methods with body }


Multiple Inheritance is not achieved in java, to overcome this problem Interface mechanism is introduced.



interface Vehicle{
	void car();
	void bike();
}

class InterfaceDemo implements Vehicle{
	public void car(){
		System.out.println("Car is running...");
	}
	public void bike(){
		System.out.println("Bike is running..");
	}
	public static void main(String[] args)
	{
		Vehicle obj=new InterfaceDemo();
		obj.car();
		obj.bike();
	}
}



Output:

Car is running...
Bike is running..