wood burning stoves*
The moose likes Beginning Java and the fly likes question related to interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question related to interfaces" Watch "question related to interfaces" New topic
Author

question related to interfaces

davidson john
Greenhorn

Joined: Mar 03, 2007
Posts: 8
Hi All,
Suppose I have two third party interfaces as given in the below example with a abstract method with same name, but different return types and my class has to implement both of them.

Example:

// First interface

interface A{

void setX();

}


//Second Interface

interface B{

int setX();

}

//Implementing Class

class C implements A, B{

public void setX(){

System.out.println("This is interface A's method");
}

public int setX(){

System.out.println("This is interface B's method");
return 0;
}


}

I have created a class as above which implements the two interfaces, but this results in compile time errors as follows

C.java:8: setX() is already defined in C
public int setX(){
^
C.java:3: setX() in C cannot implement setX() in B; attempting to use incompatible return type
found : void
required: int
public void setX(){
^
If i remove the setX() method with return type int, then it gives me one error as follows

C.java:3: setX() in C cannot implement setX() in B; attempting to use incompatible return type
found : void
required: int
public void setX(){

As both the interfaces are third party I cannot change the return type, but if my class has to implement both interfaces at any cost, Can anyone please tell me how to overcome this problem?

Thanks in advance,


Regards,
David.
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
I guess you need to have 2 implementing class /inner class and use their instances in Class C
satishkumar janakiraman
Ranch Hand

Joined: May 03, 2004
Posts: 334
Hi,

Try like this

Create a class named FirstClass implements InterfaceOne

Create a class named SecondClass implements InterfaceTwo


in your class i.e FirstClass, create an instance for your second class. i.e SecondClass object

implement InterfaceOne method and write another method test and inside of test call InterfaceTwo method which will be implemented in SecondClass.

I hope, it would solve your problem

bye for now
sat
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
The above two solutions seem to be ok. But one must also understand why this problem is occurring. First, the interfaces A and B are totally unaware of each other, so they aren't responsible for the error. The first error is because the compiler expects an overloaded method with name setX and a different arguement list. But as both methods take no arguements, it results in an error.

The second error seems to occur because the compiler confuses the method void setX(), overridding the method of interface A with that of interface B.

Enjoy!!!
Sid
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11257
    
  16

Do you have a way to know inside your Class C which method was called? or does it matter if you always run both?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
davidson john
Greenhorn

Joined: Mar 03, 2007
Posts: 8
Hi Fred,
There is no need to know which method is called, but if it matters to run both what would be the solution.

Thanks in Advance,

Regards,
David
[ March 13, 2007: Message edited by: davidson john ]
davidson john
Greenhorn

Joined: Mar 03, 2007
Posts: 8
Hi Srikanth & Satish,
Thanks for the solution, i just want to know without creating another class or without using inner class is there anyother way to get a solution for this problem.

Thanks in Advance,

Regards,
David
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi David

What happens, if you use a "trick".

interface A{

void setX();

}

interface B{

int setX(int x);

}

class C implements A, B{

public void setX(){

System.out.println("This is interface A's method");
}

public int setX(int x){

System.out.println("This is interface B's method");
return 0;
}


}

You just give an argument to the method in interface B. It works fine like that.


SCJP 1.4
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi David

The problem is only in class C. That means, inside class C you have two methods with the same name. You overload the method setX.

a) Overloaded methods MUST change the argument list.
b) Overloaded methods CAN change the return types.
c) Overloaded methods CAN change the access modifier.
d) Overloaded methods CAN declare new or broader checked exceptions.

Thus the only solution might be to use the trick I posted before. You MUST change the argument list.
suji kumar
Greenhorn

Joined: Nov 08, 2006
Posts: 10
Waefler is right..
The problem is not due to implementing 2 third party interfaces....its becuase overloaded methods
Its hardcoded rule of OOPs for overloading the method that, you can not overload the method with same argument..watever be the return type it doesnt matter, there has to be some changes in argument
davidson john
Greenhorn

Joined: Mar 03, 2007
Posts: 8
Hi Urs Waefler,
I have just one query with the solution you have given, how can anyone change a interface's method argument that is provided by a third party?(i.e as we can implement a third party interface, but cannot directly modify it)


Regards,
David
Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi David

That is what I thought too. Probably one can not change the argument of a method in a given interface. The problem is obvious. In class C there are two methods with the same name. If they have the same arguments, it does not work. To solve the problem you must start at that point.

If two methods in the same classe have the same name, they can not have the same arguments.

Me too I continue to think about a solution.

Regards
Urs
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11257
    
  16

Here's the problem, as I see it. You CANNOT have two methods with the same signiature in the same class. It doesn't matter if they come from an interface, are inherited from a class and an interface, or you just want to define two.

The signiature does NOT include the return type - just the method name, the type and order of the parameters.

If you can always do both things, I think it's easy.



when you say "implements X", all you are saying is that you will have a method in your class that matches the signitature. since the return type is not part of the signiature, you should be able to get away with this. Note that I have not tried it, so I am not SURE it will work.

when the user calls the void setX(), they can (and probably will) ignore the return value, since they aren't expecting it.

}
davidson john
Greenhorn

Joined: Mar 03, 2007
Posts: 8
Hi Fred Rosenberger,
It doesnt work as I have tried this already and it gives an compile time error saying:

setX() in C cannot implement setX() in A; attempting to use incompatible return type
found : int
required: void
public int setX(){
^

Regards,
David
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Any chance we can break the requirement in your first sentence: "my class has to implement both of them" ??

Do we have to pass the very same object to methods that require A and methods that require B? Can we have two objects that share some state, or wrap one with the other?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
davidson john
Greenhorn

Joined: Mar 03, 2007
Posts: 8
Hi Stan James,
I know that we can achieve this by having two objects which share the state or wrap one with another, but what i want to know is that is there any other way to achieve this by using a single class or object.

Regards,
David.
suji kumar
Greenhorn

Joined: Nov 08, 2006
Posts: 10
I think it is not possible using single class..because the object created using tht perticular class will not behave differently for the same messsage(in this case setX() is the meaasge)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question related to interfaces