• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

question related to interfaces

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you need to have 2 implementing class /inner class and use their instances in Class C
 
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have a way to know inside your Class C which method was called? or does it matter if you always run both?
 
davidson john
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Urs Waefler
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
davidson john
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)
 
reply
    Bookmark Topic Watch Topic
  • New Topic