Java interfaces

Sometimes it will be helpful to define a class which contains rules on what must be done by the class but not how to do it. Yes, we have learned this in an abstract method. An abstract method defines the signature of a method but does not contain any information about how to do it. A subclass has to implement this method as per its requirement. To enhance the power of abstract classes Java provides interfaces.

An interface is almost similar to the abstract class, where we can define methods with no body. These methods must be implemented in the class which implements this interface.

  • Like abstract classes, any number of classes can implement an interface.
  • Unlike abstract classes, a single class can implement any number of interfaces.
  • An abstract class can have both abstract and non abstract methods. Where as an interface only contains all abstract methods but they don’t use abstract keyword.

To create an interface we need to use interface keyword in place of class and define its methods signatures. Below is simple syntax of it:

access-modifier interface interface-name {
    returntype methodname1(paramlist);
    returntype methodname2(paramlist);
    ....
    returntype methodnameN(paramlist);
    
    
    type var1 = value;
    type var2 = value;
    ....
    type varN = value;
}

In the syntax access modifier can be either public or empty. If no modifier is used then this interface is available within that package. We will learn about packages in later sections. If you specify public access modifier then you must have a separate file with the same name as the interface name. Below is sample interface called Sequence which contains three methods such as getNextNumber, setInitialValue, and restart but it does not contain any information about how to do it.

interface Sequence {
	
	void getNextNumber();
	int  setInitialValue(int x);
	void restart();
}

Implementing an Interface

After an interface is defined, we need to create classes which implement our interface. To implement an interface we need to use implements keyword in class definition, the simple syntax is shown below,

class class_name extends super_class implements interface_name {
    // class-body
}

To implement more than one interface then each interface is separate by a comma. And more importantly, the methods we are implementing in the class must be public and should match the same signature as that of an interface. Now let us create a class Twos which implements our Sequence interface. It adds functionality to getNextNumber(), setInitialValue () and restart() methods. You can see that it uses the same signature as that of Sequence interface and all these methods are public. In addition to the methods from the interface, the class can have its own methods and data members too.

/*
This is a simple Java program about Interface.
Call this file KH_Interface.java.
*/
interface Sequence {
	
	int getNextNumber();
	int setInitialValue(int x);
	void restart();
}

class Twos implements Sequence {
	int start;
	int val;
	Twos() {
		start = 0;
		val = 0;
	}
	public int getNextNumber() {
		val += 2;
		return val;
	}
	public void restart() {
		val = start;
	}
	public int setInitialValue(int x) {
		start = x;
		val = x;
		return start;
	}
}

class KH_Interface {
	public static void main(String args[]) {
		Twos ob = new Twos();
		
		for(int i=0; i < 5; i++)
			System.out.println("Next value is " + ob.getNextNumber());
		
		System.out.println("\nResetting");
		ob.restart();
		
		for(int i=0; i < 5; i++)
			System.out.println("Next value is " + ob.getNextNumber());
		
		System.out.println("\nStarting at 100");
		ob.setInitialValue(100);
		for(int i=0; i < 5; i++)
			System.out.println("Next value is " + ob.getNextNumber());
	}
}

Download the code  Run the code

Output:

Next value is 2
Next value is 4
Next value is 6
Next value is 8
Next value is 10

Resetting
Next value is 2
Next value is 4
Next value is 6
Next value is 8
Next value is 10

Starting at 100
Next value is 102
Next value is 104
Next value is 106
Next value is 108
Next value is 110
<<< Multilevel Inheritance and ReferencingJava Interface Variables and Default Methods >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .