name conflict with multiple inheritance

I

ibondre

How do I solve name conflict with multiple inheritance.

interface I1 { void f1(); }
interface I2 { int f2(); }


class C1 implements I1, I2 { };

How can I both implemement the f1 and f2 for class C1??


I need to create MyConnection from java.sql.Connection and
javax.xml.XQConnection?


Irfan.
 
I

ibondre

Oops typo, there its function f1 for both interfaces I1 and I2...

How do I solve name conflict with multiple inheritance.


interface I1 { void f1(); }
interface I2 { int f1(); }


class C1 implements I1, I2 { };


How can I both implemement the I1.f1 and I2.f1 for class C1??


I need to create MyConnection from java.sql.Connection and
javax.xml.XQConnection?
 
E

epicwinter

what do you mean by name conflict?

class C1 implements I1, I2
{
public void f1()
{
System.err.println("F1 called");
}

public int f2()
{
System.err.println("F2 called");
return -1;
}
}
 
I

ibondre

I mistyped the question...

interface I1 { void f1() }
interface I2 { int f1() }

class C1 implements I1, I2
{
// How do I implmenent int f1() and void f1()?


// is there a way to say that I am implment ing I1. void f1 and I2 int
f1?
}
 
R

Ross Bamford

How do I solve name conflict with multiple inheritance.

Stop thinking C(++) :) When you implement interfaces, you're not really
inheriting anything (except arguably constants but thats a whole other
story) - what you are doing is saying "It's safe to treat my class as an
instance of this interface, because it defines at least all the methods
defined in it".

Thus you see there is no such thing as a name conflict - two interfaces
may define that a class must have a method called "f1" in order to make
it safe to use it's object's as instances of that interface, and there
is no conflict.

The downside of this is that you'll see now that you cannot implement
different versions of a method in the same class to suit different
interfaces, and you can't (correct me if I'm wrong?) tell directly
whether a method was called directly on your class, or through an
interface, or what. All you have is the standard method differentiation.

Of course with different method signatures the issue is moot, since from
the point of view of the interface you would only 'see' the one
overload.

Cheers,
Ross
 
L

Lee Fesperman

ibondre said:
I mistyped the question...

interface I1 { void f1() }
interface I2 { int f1() }

class C1 implements I1, I2
{
// How do I implmenent int f1() and void f1()?

// is there a way to say that I am implment ing I1. void f1 and I2 int
f1?
}

Nope, there is no way.
 
P

Patricia Shanahan

ibondre said:
Oops typo, there its function f1 for both interfaces I1
and I2...

A single class can't implement both interfaces, if they
contain methods with the same name and parameter list,
unless the methods are identical in return type and required
semantics.

Is it necessary for MyConnection to implement both I1 and I2
directly, rather than having methods that return objects
implementing each interface?

Patricia
 
D

Dimitri Maziuk

Ross Bamford sez:
....
Thus you see there is no such thing as a name conflict - two interfaces
may define that a class must have a method called "f1" in order to make
it safe to use it's object's as instances of that interface, and there
is no conflict.

You missed the return types. In a language that allows discarding
function return value it is impossible to overload on return type
-- or to implement both of those interfaces.

Dima
 
R

Ross Bamford

Ross Bamford sez:

You missed the return types. In a language that allows discarding
function return value it is impossible to overload on return type
-- or to implement both of those interfaces.

Dima

Aha! You've got me there :)

I really must learn to read things *properly*.

Ross
 
B

Bjorn Abelli

...
How do I solve name conflict with multiple inheritance.

interface I1 { void f1(); }
interface I2 { int f1(); }

class C1 implements I1, I2 { };

How can I both implemement the I1.f1 and I2.f1 for class C1??

I need to create MyConnection from java.sql.Connection and
javax.xml.XQConnection?

That's not really comparable with your example, is it?

There's no name conflict between the operations in java.sql.Connection and
javax.xml.XQConnection with primitives or void as return types.

There are some other operations, that return interface types that differ.

I haven't delved too deep into it, but I think it could be possible to solve
it, though it would be much work, and it *could* be other unresolvable
issues (name conflicts, etc) down the line...

Just to give an example:

In both java.sql.Connection and javax.xml.XQConnection you have the
operation getMetaData, but which returns different types, DatabaseMetaData
and XQMetaData.

So what do you want to return from that?

Just as you "need" to implement java.sql.Connection and
javax.xml.XQConnection into one class, you now "need" to create a type that
implements both DatabaseMetaData and XQMetaData.

class MyMetaData implements DatabaseMetaData, XQMetaData
{
...
}

....and the implementation for your MyConnection could be...

public class MyConnection implements Connection, XQConnection
{
...
public MyMetaData getMetaData() { ... }
...
}

But as I said, there might be other unresolvable issues further down the
line, as I just took a quick glance on it...

// Bjorn A
 
D

Dimitri Maziuk

Ross Bamford sez:
Aha! You've got me there :)

I really must learn to read things *properly*.

Heh. Mee too. Actually, in this paticular case it *is* possible
-- just redeclare void method as int and ignore its return value.
It's only not possible in a general case.

Dima
 
R

Ross Bamford

Ross Bamford sez:

Heh. Mee too. Actually, in this paticular case it *is* possible
-- just redeclare void method as int and ignore its return value.
It's only not possible in a general case.

Dima

True, but IMO this is a bug in the language. I know it's legal and even
makes sense, but it has the potential to thoroughly perplex even an
experienced developer. It's too easy to innocently change the context
during wider-scale refactoring and end up in that worst of worst-cases,
the old "No error report, it just doesn't work properly" situation :(

Ross
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,162
Latest member
GertrudeMa
Top