Java Interface Variables and Default Methods

Java interface aariables

We can declare variables in Java interfaces. By default, these are public, final and static. That is they are available at all places of the program, we can not change these values and lastly only one instance of these variables is created, it means all classes which implement this interface have only one copy of these variables in the memory. The below example demonstrates:

/*
This is a simple Java program about Interface.
Call this file KH_InterfaceVariables.java.
*/
interface X {
	int max = 10;
}

class Example implements X {
	public void getMax(){
		System.out.println(max);
	}
}

class KH_InterfaceVariables {
	public static void main(String args[]) {
		Example ob = new Example();		
		ob.getMax();		
		//ob.max = 20; //uncommenting this will generate compile error
	}
}

Download the code Run the code

Output:

10

Even though we can create variables in interfaces, but in general we rarely use this.

Default Methods

With respect to interfaces we have one major rule: A class must implement all methods which are declared in the interface. But after some years of usage we got a big issue. Consider we have created an interface and it became very famous and so many people have used this for their own implementation. But you have to change this interface to add a new method after some years. Then all people who have used our interface has to change their source to match with our interface. This is practically very difficult because all previous implementations were done some years back. So they might not have source code with them, then they can not use your latest interface.

To avoid this problem Java has introduced default method to an interface in JDK 8. This means that we can write our own default implementation to our interface methods. Now any class which is implementing this interface need to implement default method. If default method is not implemented in class then default implementation in the interface is executed. If any class implements default method then this implementation hides interface default implementation. To make a method and implementation as default then we need to use default keyword.

For example, we change setInitialValue() method of Sequence interface. We make it default method by adding default keyword and write body to it so that it returns 1 as default. Now we do not implement this method in Twos class. For more understanding try to add setInitialValue method in a class and see how it works. One point to remember is, to use this feature you should be working with JDK 8 and above.

/*
This is a simple Java program about Interface.
Call this file KH_InterfaceDefault.java.
*/
interface Sequence {
	
	int getNextNumber();
	default int setInitialValue(int x) {
		return 1;
	}
	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;
	}
	
}

class KH_InterfaceDefault {
	public static void main(String args[]) {
		Twos ob = new Twos();
		
		ob.start = ob.val = ob.setInitialValue(0);
		
		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 3
Next value is 5
Next value is 7
Next value is 9
Next value is 11
<<< Java interfacesJava Interface References and Extending Interfaces >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .