No announcement
 

Interfaces

An interface is a reference type in java. It is a fully abstract class (i.e., it does not contain any implemented method.)

An interface is declared with an interface keyword and is used by a class with implements keyword in its declaration.

Example:

InterfaceDemo.java

package com.codingnous.tutorial;

public class InterfaceDemo {

	public static void main(String[] args) {

		// Create class object.
		Arithmetic a = new Arithmetic();

		// Invoke the implemented methods
		System.out.println("Square of 28 is " + a.square(28));
		System.out.println("Sum of 40 and 54 is " + a.sum(40, 54));
	}
}

// Declare an interface.
interface Operation {

	// Declare two abstract methods.
	abstract int square(int num);

	abstract int sum(int num1, int num2);
}

// Declare interface implementing class.
class Arithmetic implements Operation {

	@Override
	public int square(int num) {
		return num * num;
	}

	@Override
	public int sum(int num1, int num2) {
		return num1 + num2;
	}

}

Output
Square of 28 is 784 Sum of 40 and 54 is 94