aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How to handle Interfaces with same methods ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "How to handle Interfaces with same methods ?" Watch "How to handle Interfaces with same methods ?" New topic
Author

How to handle Interfaces with same methods ?

Trupti Samel
Greenhorn

Joined: May 15, 2001
Posts: 24
interface List
{
int number =100;
public void SetCriticalParameter();
}
interface Stack
{
int number =10;
public void SetCriticalParameter();
}

public class EndClient implements List,Stack
{
public static void main(String[]args)
{
EndClient c = new EndClient();
System.out.print(List.number);
System.out.print(Stack.number);
// c.List.SetCriticalParameter() ; ???
// c.Stack.SetCriticalParameter() ; ???
}
/*
public void SetCriticalParameter()
// How to say this is for List
{
// ... Blah Blah Blah
}
public void SetCriticalParameter()
// And this is for Stack ?
{
// ... Blah Blah Blah
}
*/
}
Here in this code we could access the values of varibales with similar names from the two interfaces with help of the interface names. But, AFAIK, we can not do the same thing for methods. Whats the Java way for accessing and defining these methods ?
From my reading so far, I came across asbtract classes which handle similar situations, but I believe that they just skirt the issue. C++ handles this using multiple implementation inheritance, but we don't want to do that either. What then is the best way to take care of this situation ?
Could somebody clear this for me ?
Thanks,
Trupti.

Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
From JLS, "it is permitted for a single method declaration in a class to implement methods of more than one superinterface."
IMHO it is just bad design if both the interfaces has the same method!
------------------
Ajith Kallambella M.
Sun Certified Programmer for the Java�2 Platform.
IBM Certified Developer - XML and Related Technologies, V1.


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
All an interface does is require that you must implement a method. How you implement it is completely up to you. However, your example is not quite meaningful. All variables declared in an interface are public, static, and final. Plus, "Every field in the body of an interface must have an initialization expression, which need not be a constant expression." So the variables in an interface can never be changed.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Cameron Park
Ranch Hand

Joined: Apr 06, 2001
Posts: 371
Hi, if you have an instance of EndClient, how can you call number from, say, List?
Jason Li
Ranch Hand

Joined: Jun 24, 2001
Posts: 46
All variables declared in an interface are public, static, and final. So it is simply to use the constant in the interface:
List.number.
Jason


green horn
Trupti Samel
Greenhorn

Joined: May 15, 2001
Posts: 24
Hello All,
Thanks for replying to my question. I guess Ajith is the only one who understood what I am asking, so let me elaborate on my question again.
I know that this would be considewred bad design, but, I have two interfaces which declare a method with the exact same signature. I have to implement this method in a class I derive from both these interfaces, but, offcourse, I can not have one single method which can do the work of both these interface methods. List's SetCriticalParameter() requires a different set of rituals to be done, as compared to Stack's SetCriticalParameter() method. Also, on a similar note, how would a client of my class invoke one method as opposed to the other ?
So, my question is, how can this be done. The JLS, as Ajith pointed out, says that this is impossible if the two methods have same signature, but different return types. This led me to the question at hand.
Thanks for your inputs,
Trupti.
Trupti Samel
Greenhorn

Joined: May 15, 2001
Posts: 24
Hi Thomas,
Thanks for your reply. I agree with your review of my code, however, Khalid, Section 6.4, page 199, states that all Interface variables are implicitly public static final, whether you specify it or not. Thats the reason why I had omitted these keywords.
However, on a different note, the question I am trying to portray is a bit different, it being : variables can be accessed with the interface name, so same variable names can still be resolved. Whats the corresponding way of resolving method names ?

Thanks again,
Trupti.

Originally posted by Thomas Paul:
All an interface does is require that you must implement a method. How you implement it is completely up to you. However, your example is not quite meaningful. All variables declared in an interface are public, static, and final. Plus, "Every field in the body of an interface must have an initialization expression, which need not be a constant expression." So the variables in an interface can never be changed.

Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Trupti, I think you're misunderstanding how interface implementation works. You do not need to have one method to "do the work of both these interface methods". The interface methods are abstract and don't do any work; by implementing the interface, you agree to define the work to be done by any methods the interface declares. If you are implementing 2 interfaces, and both declare a method with the same signature, then you will be satisfying the contract for both interfaces with a single method.
I think what you really want to do is to define methods with the same name but which do different things. You can do this by overloading methods with the same name but different parameters.
I don't think the structure you have set up (2 interfaces with identical method declarations and identical field names) will help you accomplish what it sounds like you want to do. Implementing the second interface is not providing any useful functionality, and in fact will cause you to get compiler errors if you attempt to reference the number field (unless you make an explicit cast to the interface type).
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Trupti Samel:
However, on a different note, the question I am trying to portray is a bit different, it being : variables can be accessed with the interface name, so same variable names can still be resolved. Whats the corresponding way of resolving method names ?
I think you missed the point. What difference does it make which method I reference? Neither of them do anything.
So let's take an example. Let's say I have two interfaces, X and Y both of which have an add() method. I need to code an add() method. It can do nothing or it can do something that is specific to one of the two interfaces. If I have it do nothing, I could have addX() and addY() methods.
I have to admit that I have never encountered this problem and I find it a stretch to think of a situation where this might cause a problem.
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4115
    
    2

Trupti, it seems to me that the best course of action would be to redesign EndClient, perhaps splitting it into two classes: one implements List and the other implements Stack. I cannot think of any way to do what you want directly in Java without resorting to kludges.

Junilu - [How to Ask Questions] [How to Answer Questions] [MiH]
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to handle Interfaces with same methods ?
 
Similar Threads
Sun Cirtification
need some help please with project
Doubt in multiple inheritance
Requirement of Interface in Java
Interfaces