Java Interface References and Extending Interfaces

Interface References

As you know we can not create objects of an abstract class, but we can create objects of an interface. And we can refer this object to any object of a class which implements its interface. When you call a method of the interface by using interface reference object, the method defined in the class which implements this interface gets executed. We update our SequenceDemo program so that it explains this concept.

In the below program, we have created a reference to interface Sequence and assigned to an object of Twos class. When we call getNextNumber method it executes getNextNumber defined in Twos class. As a practice for you try to create Threes class which implements Sequence interface and use our interface reference object to call methods of Twos and Threes alternatively.

/*
This is a simple Java program about Interface.
Call this file KH_InterfaceReference.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_InterfaceReference {
	public static void main(String args[]) {
		Sequence ref;
		Twos ob = new Twos();
		
		ref = ob;
		
		for(int i=0; i < 5; i++)
			System.out.println("Next value is " + ref.getNextNumber());
		
		System.out.println("\nResetting");
		ref.restart();
		
		for(int i=0; i < 5; i++)
			System.out.println("Next value is " + ref.getNextNumber());
		
		System.out.println("\nStarting at 100");
		ref.setInitialValue(100);
		for(int i=0; i < 5; i++)
			System.out.println("Next value is " + ref.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

Extending Interfaces

We can also derive one interface from another interface using extends keyword. And the class which implements derived interface must implement all methods of all interfaces. In the below example interface X defines two methods func1 and func2. The next interface Y which extends from interface X defines one method func3.

Now if a class Example implements interface Y. As Y extends from X, class Example has to implement all three functions func1, func2, and func3. Just to confirm our discussion, remove func1 method from class Example and try to execute and it will spit compile errors..

/*
This is a simple Java program about Interface.
Call this file KH_InterfaceExtends.java.
*/
interface X {
	
	void func1();
	void func2();	
}

interface Y extends X{
	
	void func3();
}

class Example implements Y {
	public void func1(){
		System.out.println("Function 1");
	}
	public void func2(){
		System.out.println("Function 2");
	}
	public void func3(){
		System.out.println("Function 3");
	}
}

class KH_InterfaceExtends {
	public static void main(String args[]) {
		Example ob = new Example();
		
		ob.func1();
		ob.func2();
		ob.func3();
	}
}

Download the code  Run the code

Output:

Function 1
Function 2
Function 3
<<< Java Interface Variables and Default MethodsJava Exception Handling >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .