2 interface with 2 same function prototype but different return type

C

cai junfu

interface a{
int g();
}

interface b{
double g();
}

class c implements a, b{
}


how should class c's implementation of the two interface be written?
is it possible at all?
 
X

xarax

cai junfu said:
interface a{
int g();
}

interface b{
double g();
}

class c implements a, b{
}


how should class c's implementation of the two interface be written?
is it possible at all?

Cannot happen in Java. The method return types
must be the same (ignoring the Generics thing).

You can do an alternative using objects like this:

public interface A
{
public abstract Number g();
}

public interface B
{
public abstract Number g();
}

public class C
implements A,B
{
public Number g()
{
Integer a;
Double b;
Number result;

if(/* some condition */)
{
a = new Integer(12);
result = a;
}
else
{
b = new Double(20.5);
result = b;
}
return result;
}

The caller of C.g() must then decide how to
extract the value of the Number result.
 
S

steve

Cannot happen in Java. The method return types
must be the same (ignoring the Generics thing).

You can do an alternative using objects like this:

public interface A
{
public abstract Number g();
}

public interface B
{
public abstract Number g();
}

public class C
implements A,B
{
public Number g()
{
Integer a;
Double b;
Number result;

if(/* some condition */)
{
a = new Integer(12);
result = a;
}
else
{
b = new Double(20.5);
result = b;
}
return result;
}

The caller of C.g() must then decide how to
extract the value of the Number result.

you can return a different "type", just cast the type to an object, return
an object,
then in your main calling code do a test for "instance of" XX or instance of
YY.
 
J

Joona I Palaste

you can return a different "type", just cast the type to an object, return
an object,
then in your main calling code do a test for "instance of" XX or instance of
YY.

Isn't that exactly what xarax did in the above code?
However, I'd write it the following way:

public interface A {
public Number g();
}

public interface B {
public Number g();
}

public class C implements A, B {
public Number g() {
if (/* some condition */) {
return new Integer(12);
}
else {
return new Double(20.5);
}
}
}

Note that explicitly casting the returned object reference to a
superclass type is usually not needed. In fact I have written code
similar to the above and only had to use upcasting in a ?: expression
where neither of the returned references was a supertype of the other.
 
X

xarax

Joona I Palaste said:
Isn't that exactly what xarax did in the above code?
However, I'd write it the following way:

public interface A {
public Number g();
}

public interface B {
public Number g();
}

public class C implements A, B {
public Number g() {
if (/* some condition */) {
return new Integer(12);
}
else {
return new Double(20.5);
}
}
}

Note that explicitly casting the returned object reference to a
superclass type is usually not needed. In fact I have written code
similar to the above and only had to use upcasting in a ?: expression
where neither of the returned references was a supertype of the other.

I was writing extremely verbose example so that
the casual/newbie reader could see what was happening.

Also, I come from a Structured Programming background
(from the days before Object Oriented Programming), so
I always insist that my code have only one "return"
statement. Your mileage may vary.

btw: One of the nice things about Number is that
the caller doesn't really need to use "instanceof",
because it has the various methods for extracting
the value as various primitives. So, just use the
Number instance without caring about the actual
subtype. (Polymorphism is fabulous.)
 
G

Guest

interface a{
int g();
}
}
interface b{
double g();
}
}
class c implements a, b{
}
}

how should class c's implementation of the two interface be written? is it
possible at all?

In Java, you cannot specify multiple methods which vary only by return
type.

If the semantics of a.g() is different from b.g(), I would not recommend
implementing both interfaces.

If a.g() and b.g() have the same semantics, consider moving g() to a new
interface extended by a and b if their return types are compatible (in
this case, Number would be reasonable).

HTH,
La'ie Techie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top