What is the meaning of two interfaces with same methods having same signature but different return t

In object-oriented programming, an interface is a set of method signatures that a class must implement if it wants to conform to that interface. An interface defines the structure of an object, but not its implementation. A class can implement multiple interfaces, each with its own set of method signatures.

When two interfaces have the same method signature but different return types, it means that the methods are structurally identical in terms of their input parameters and method name, but their return type differs. This can be useful in scenarios where you want to have two different behaviors for the same method signature.

For example, consider two interfaces, "IReadable" and "IWritable", each with a "read" method signature. The "read" method in the "IReadable" interface could return a value of type "string", while the "read" method in the "IWritable" interface could return a value of type "int". Even though the method name and input parameters are the same, the return types are different, and thus the methods have different behavior and purpose.

Implementing these interfaces in a class allows the class to have two different implementations of the "read" method, depending on which interface it is implementing. This can be useful in scenarios where you want to use the same method name but have different behavior based on the context.

public interface InterfaceX
{
	public int geek();
}
public interface InterfaceY
{
	public String geek();
}

Now, Suppose we have a class that implements both those interfaces:

public class Testing implements InterfaceX, InterfaceY
{
public String geek()
	{
		return "hello";
	}
}

The question is: Can a java class implement Two interfaces with same methods having an equivalent signature but different return types??
No, its a mistake
If two interfaces contain a way with an equivalent signature but different return types, then it's impossible to implement both the interface simultaneously.
According to JLS (§8.4.2) methods with the same signature aren't allowed during this case.

Two methods or constructors, M and N, have the same signature if they have,
the same name
the same type parameters (if any) (§8.4.4), and
after adopting the formal parameter types of N 
 to the type parameters of M, the same formal parameter types.

It is a compile-time error to declare two methods with override-equivalent signatures in a class.

// JAVA program to illustrate the behavior
// of program when two interfaces having same
// methods and different return-type
interface bishal
{
public
	void show();
}

interface geeks
{
public
	int show();
}

class Test implements bishal, geeks
{
	void show() // Overloaded method based on return type, Error
	{
	}
	int show() // Error
	{
		return 1;
	}
public
	static void main(String args[])
	{
	}
}

 

Submit Your Programming Assignment Details