Generic arguments in a Method on an interface.

Hi all,
I'm having trouble with the following code:
Basically this is what i want:
the Classes:
public class SuperClass
public class ClassA extends SuperClass
public class ClassB extends SuperClass
public class ClassC extends SuperClass
The interface
public interface interfaceX
   public methodX(SuperClass A);
Implementation of the interface
public class classX implements interfaceX
     public methodX(ClassA A)
}My problem is on the declaration of the interface interfaceX in the class classX.
How can i do such thing?
I'm trying to avoid the "correct" declaration. this is:
public methodX(SuperClass A)
        { ... }because I have a couple of class that extends that interface with deferents arguments, all of them extending from the SuperClass.
I know i could use the instanceof keywork, but i'm trying to avoid the extra checking...
Thanks in advance for any help!!
Edited by: ValdemarP on Mar 29, 2010 1:55 AM

i know I'm changing the contract, but only the arguments. if you notice, the new arguments is child of class SuperClass... How can I do this? Declare an method in an interface that accepts an arguments of some class and implement that interface, with argument that are children of the arguments declared in the interface... hope i was more clear this time... :(
Example. I can do this on an interface:
public void MethodListGeneric(List<? extends SuperClass> xpto )and during the implementation of the interface, I can declare the method using any arguments for the generic list, that extends SuperClass.
Example:
public class xpto extends InterfaceX
    public void MethodListGeneric(List<ClassA> xpto )
     // in here I've use ClassA insted of SuperClass
}Edited by: ValdemarP on Mar 29, 2010 3:15 AM
Edited by: ValdemarP on Mar 29, 2010 3:15 AM

Similar Messages

  • Overriding abstract methods with generic argument

    Hi,
    This post is similar to the one posted just before it :
    [http://forum.java.sun.com/thread.jspa?threadID=5281500|http://forum.java.sun.com/thread.jspa?threadID=5281500]
    I am trying to override an abstract method which has a generic argument and a problem occurs. I would like to find out whether what I am trying to do is not legal. The point of interest here is that the overriding class reduces the scope of the abstract class.
    The Abstract class AbstractAdapter defines accessors for a collection containing objects derived from AbstractPort. (<? extends AbstractPort>)
    The overriding class Adapter overrides these accessors but reduces the scope of the collection objects from <? extends AbstractPort> to <Port>. Port
    Here is my abstract class:
    public abstract class AbstractAdapter {
         public abstract Collection<? extends AbstractPort> getPorts();
         public abstract void setPorts(Collection<? extends AbstractPort> ports);
    }And here is the class that overrides it:
    public class Adapter extends AbstractAdapter {
         public Collection<Port> getPorts() {
              return null;
         public void setPorts(Collection<Port> ports) {
    }The Port class is shown here:
    public class Port extends AbstractPort {
    }Edited by: BenDave on Apr 2, 2008 4:39 AM

    It's not really the same.
    I don't have a compiler to hand, so this is off the top of my head. I expect you want something like this:
    public abstract class AbstractAdapter<T extends AbstractPort> {
         public abstract Collection<T> getPorts();
         public abstract void setPorts(Collection<T> ports);
    public class Adapter extends AbstractAdapter<Port> {
         public Collection<Port> getPorts() {
              return null;
         public void setPorts(Collection<Port> ports) {
    }

  • How to create a method with an interface parameter?

    Hi there.
    I would create a method with an interface parameter. I mean this:
    public interface MyInt {
        public void method(int i);
    public class SubClass implements MyInt {
        public void method(int i) {
            System.out.println("The number is: " + i);
    public class MainClass {
        public MainClass(MyInt inter) {
            inter.method(10);
        public static void main(String[] args) {
    (*)     new MainClass(new SubClass());
    }I tried to compile this but do not work (incompatible types on (*)) . Is it possible to do something like that or not?

    I compiled and runned this code on my machine and it works fine.
    How did you did to compile and run this code ?
    Did you put each code on its own file or all codes in the same file ?
    I did like this:
    I put each code in its own file and put all in the same dir, after I did:
    javac -classpath . MainClass.java
    After that, I did:
    java -classpath . MainClass
    I hope this help you
    Lucas

  • Method in abstract interface

    Suppose I have an abstract interface which has one method:
    public abstract interface IParent {
      public void setSignal(int signal);
    }And I have a concrete interface inherited from IParent which also have one method:
    public interface IChild extends IParent {
      public void setSpeed(int speed);
    }If I define a class which implements IChild it only asks me to implement setSpeed method.
    public class Test implements IChild {
    }Why it does not ask me to implement setSignal method of IParent interface?

    Suppose I have an abstract interface which has one method:
    Every interface is implicitly abstract.
    public void setSignal(int signal);
    Every method declaration in the body of an interface is implicitly public.
    And I have a concrete interface inherited from IParent which also have one method:
    Every interface is implicitly abstract.
    If I define a class which implements IChild it only asks me to implement setSpeed method.'It' being what?
    Why it does not ask me to implement setSignal method of IParent interface?I have no idea what 'it' you're talking about, but the Test class will not compile.

  • Getting return value of a method in an interface

    Hello! I have a problem. I have an interface called I. One of the declared method in this interface is M. And I have a class called C. Class C declares Interface I. I want to get the return value of the method M. I used invoke method. M.invoke(anInstance, arg[]). You can't take the instance of interfaces or abstract classes. If I use a class instead of instance for anInstance variable, i get "java.lang.IllegalArgumentException: object is not an instance of declaring class" error. If I use an instance, i get "java.lang.IllegalAccessException: Can not call newInstance() on the Class for java.lang.Class" error. This is because of getting an instance of an interface. if you can help me. i will be greatfully happy.

    Thanks for the feedback Tom although you should rethink what you said.
    I did not trash your suggestion, nor did I make any reference to it.
    Perhaps you should read the OP to see where you went wrong.
    OP wants to know return type of Interface's methodNo, the OP wants to obtain the return value.
    AND OP knows that instantiation won't work.He was trying to instantiate an Interface, which is not possible. In order for him invoke a Method he needs a concrete implementation. I was simply pointing out what he needs to do in order to acheive his aim.
    b) there may be no known implementing classes at
    runtimeIf there is no implementing class provided then he will not be able to invoke a method on its instance.
    c) even if there were such a class, instantiating it
    may have unwanted side effects, takes unnecessary
    timeWhy is that a problem? One reason for invoking M is to see what happens.
    the class may have N methods while the interface
    has only one, so you might look for the correct method
    too longIf you bothered to study the code I supplied I obtain a Method from the Interface class so there is no problem in "looking" for a method, besides which there can be exactly 1 method with a given signature in any class so there is no searching involved.
    On a personal note, such negative replies are neither helpful to the OP and can cause a credibility problem caused by being labelled a Troll.

  • Static method allowed in interface?

    Hello,
    I've written the following code:
    package a.b.c.d;
    import java.util.Map;
    public interface Adapter
    public void execute(Mapping mapping) throws Exception;
    public static Map getParameters( ) throws Exception;
    My compiler complains thusly:
    "Adapter.java": Error #: 217 : modifier static not allowed here at line 8, column 23
    Is it illegal then to declare a static method in an interface and if so why? Thanks in advance!
    -Exits

    An interface itself is just a contract that says "I will implement thus-and-such methods with such-and-such a signature." The interface has no "meat" to it, so it can't implement the static.
    Now you're thinking, if MyClass implements MyInterface, then MyClass can just have a static method that is the one on MyInterface. The problem is with how you're getting your MyInterface-type object. You can do it like this:
    MyInterface obj = new MyClass();
    obj.myStaticMethod(); //Bzzzzzzzt! blows up at compile time...or...
    MyInterface obj = someMethodThatReturnsClassImplementingMyInterface();
    obj.myStaticMethod(); //Bzzzzzt! same problemYou can't call a static method on an instance; you have to call it on the class,
    MyClass.myStaticMethod();You can't call it on the interface directly because, again, the interface has no guts inside it. There's really no reason to put a static method on an interface because that's not what interfaces are for. If MyClass needs a static, put it there.

  • Method definition in Interfaces

    How we are able to invoke a method of an interface in a class? For example, we are invoking the method next() of the ResultSet interface in JDBC classes. How this is possible since we are not implementing the method anywhere? Please send us your valuable answer as soon as possible Thanks in advance.
    Shanil Kumar

    Actually a featur of Java thats growing on me more and more each day!!
    The idea is the interface and the implementation are two distinct but related features....
    Given that a single interface may be implemented by many classes, for what ever purpose, you can cast any instance of these implementing classes to the interface and work with it, calling methods on it, without actually knowing what implementation is actually being used!!!
    For example I will currently be coding a simple interface for logging. MyLogger just say. it will define basic methods available in any logging tool. The idea is that I can then implement the interface to interact with a particular logging famework or tool, such as JLogger for example. I may use three differnet such Logging tools, which may have completely different ways of working.
    I can now build code using my interface,
    and switch the actual logging utility that is really being used....
    Its a bit difficult to explain, but if you play arround with it youll get to understand it...
    The key point to understand is that your invoking methods on an instance that implements the interface...
    MyLoggeer logger = new JLoggerImplementation();
    here a concrete implementation is asigned to the interface/
    I can now call methods of the interface, or more precisely the implementing instance.....
    Hope this helps.
    J.Prisco

  • Is there a quick fix option for implementing the unimplemented methods for an interface?

    Hi guys,
    It's a useful feature and it seems to exist in raw Eclipse.  I'm aware of the Source -> Override/Implement methods, but this is clunky when you could hover over an interface and hit Ctrl+1 and auto-implement all methods  for that interface (which seems to be pretty much most use cases for me).  At the moment the quick-fix only seems to have renaming options.  Is there something wrong with my configuration?
    If not, is there a way to make feature requests?  :-))
    Thanks!
    Jarrod

    I have safari on my iPone and like it there... thought that I would give it a try. Well, IE just started to slow to a paint drying speed and then wouldn't load anything. Safari was installed but crashed in threee seconds each occuarnce. No reason, no error message.
    I downloaded (eventually) Firefox... loaded it, chose my extentions and have never had a problem with it since. I unloaded 5 times (gave up on) Safari, reset IE, reinstalled the extentions for that and now it is working after cleaning all of the files, passwords, forms autocomplete, temp files, etc.
    Firefox works and all of my freinds and kids use it. That is why it is so good. Remember, if your car worked like your PC you wold junk it right away. Reliability is not in the MS dictionary. It is something that you have to add via tons of fixes, add-ons and program balancing so that conflicts don't occer. I want my MAC, I want my, I want my MAC...

  • Generic Extractor using Function module with Complex Interface

    Hi,
    Has anyone created Generic extractor using Function module with Complex Interface?
    What is the difference between Complex and Simple interface in Function module?
    Pls explain.
    Thanks,
    Gopal

    Hi,
    Go through this link.....
    https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/docs/library/uuid/d3219af2-0c01-0010-71ac-dbb4356cf4bf
    GTR

  • Thread Safety : Marking arguments of  static method as final

    Hi,
    I have a class with static method. This class will be hit by several threads and I need to make the code thread safe. If I declare the arguments of this method as "final", will it guratnee that these variables cannot be changed by another thread calling this method?
    Thanks in advance,
    Rtameh

    No, the only condtion where parameters or local variables might be shared with another thread is if you create a local class which uses them, and the compiler won't allow you to do that if they aren't final.
    Local variables are always thread-safe.

  • Methods to find interface

    Hi,
    what are the methods to find interface....
    Thanks in Advance.
    regards,
    kranthi.

    Hello,
    Classes are composed by methods and attributes, some of these methods are in interfaces that are shared among classes.
    I suggest you to take the class name and go to transaction SE24 in this you'll find the interfaces tab, and in this you can see the interfaces that compose the class.
    Another way is go to the methods tab and find methods compose by a <interface_name>~<method>.
    Regards,

  • Maximum # of arguments for a method

    Is there a maximum # of arguments for a method?
    I have the following signature for a method:
    void process_l1_queue(int processnum, Scheduler level1[], Scheduler[] level2[], Scheduler[] level3[],Scheduler[] level4[],int index,int l2,int l3,int l4)when I call it with the following statement,
    level1.process_l1_queue(process_count, level1,level2,level3,level4,l1ptr, l2ptr, l3ptr,l4ptr);
    I get a cannot resolve symbol error pointing at the comma after l3ptr.
    I've deleted the l4ptr and the same error points to the ).

    wow. People can often be a bit too inflammatory with their replies here, but, at the risk of sounding like so, I would fire you on the spot if you develop an application class with that signature. There is no defense for such a signature- put some of those arguments in the constructor (this instance will always be of a certain calendar type, dollar currency), subclass- divide the functionality into smaller units. That signature is too complex and it will make every thing more difficult, particularly if some one else has to manage the code in the future. I would say, as a general rule, if you find yourself asking, will java let me put some thing this mammoth in it the way I intend, you're often using a poor design. All things being equal, you shouldn't normally have to test the limits of the JLS.

  • How to consume a method on the interface controller from another component?

    Hello,
    I have a project which contains two Components, A & B.
    I have exposed a method at the interface controller of Component A. How can I consume it from Component B?

    Hi,
       First add ComponentA as a used webdynpro component in ComponentB. Then open the Interface Controller of B and switch to Properties Tab. Select Add in the "Required Controllers" section. From the dialog that appears, select A's interface controller. Then you can access A's method from B's interface controller as:
    wdThis.wdGetAComponentInterface().<method>;
    Regards,
    Satyajit.

  • Which method of two interface are being overriden

    Hi all,
    I have a query.
    There are two interfaces interface one interface two. and both declare a method void common()
    A class three implements one, two
    and implements the method common()
    void common() {
    System.out.println(" which interface");
    so in this case the common method of which interface is being called there
    Thanks

    Hi,
    I don't understand your question. The implementation
    of a method in a class does not belong to an
    interface. You only say that you fulfill a contract,
    so the method fulfills the contract of both one and
    two.
    One thing you have to be careful of though: The method fulfills both contracts to the extent that the compiler can check. However, the API docs that describe the semantics of the methods may be radically different, and it might be impossible or nonsensical to fulfill both.

  • Using a non-static vector in a generic class with static methods

    I have a little problem with a class (the code is shown underneath). The problem is the Assign method. This method should return a clone (an exact copy) of the set given as an argument. When making a new instance of a GenericSet (with the Initialize method) within the Assign method, the variables of the original set and the clone have both a reference to the same vector, while there exists two instances of GenericSet. My question is how to refer the clone GenericSet's argument to a new vector instead of the existing vector of the original GenericSet. I hope you can help me. Thanks
    package genericset;
    import java.util.*;
    public class GenericSet<E>{
    private Vector v;
    public GenericSet(Vector vec) {
    v = vec;
    private <T extends Comparable> Item<T> get(int index) {
    return (Item<T>) v.get(index);
    public static <T extends Comparable> GenericSet<T> initialize() {
    return new GenericSet<T>(new Vector());
    public Vector getVector() {
    return v;
    public static <T extends Comparable> GenericSet<T> insert (GenericSet<T> z, Item<T> i){
    GenericSet<T> g = assign(z);
    Vector v = g.getVector();
    if (!member(g,i))
    v.addElement(i);
    return g;
    public static <T extends Comparable> GenericSet<T> delete(GenericSet<T> z, Item<T> i){
    GenericSet<T> g = assign(z);
    Vector v = g.getVector();
    if (member(g,i))
    v.remove(i);
    return g;
    public static <T extends Comparable> boolean member(GenericSet<T> z, Item<T> i) {
    Vector v = z.getVector();
    return v.contains(i);
    public static <T extends Comparable> boolean equal(GenericSet<T> z1, GenericSet<T> z2) {
    Vector v1 = z1.getVector();
    Vector v2 = z2.getVector();
    if((v1 == null) && (v2 != null))
    return false;
    return v1.equals(v2);
    public static <T extends Comparable> boolean empty(GenericSet<T> z) {
    return (cardinality(z) == 0);
    public static <T extends Comparable> GenericSet<T> union(GenericSet<T> z1, GenericSet<T> z2) {
    GenericSet<T> g = assign(z1);
    for(int i=0; i<cardinality(z2); i++) {
    Item<T> elem = z2.get(i);
    insert(g, elem);
    return g;
    public static <T extends Comparable> GenericSet<T> intersection(GenericSet<T> z1, GenericSet<T> z2) {
    GenericSet<T> g = initialize();
    for(int i=0; i<cardinality(z2); i++) {
    Item<T> elem = z2.get(i);
    if(member(z1, elem))
    insert(g, elem);
    return g;
    public static <T extends Comparable> GenericSet<T> difference(GenericSet<T> z1, GenericSet<T> z2) {
    GenericSet<T> g = initialize();
    for(int i=0; i<cardinality(z1); i++) {
    Item<T> elem = z1.get(i);
    if(!member(z2, elem))
    insert(g, elem);
    for(int i=0; i<cardinality(z2); i++) {
    Item<T> elem = z2.get(i);
    if(!member(z1, elem))
    insert(g, elem);
    return g;
    public static <T extends Comparable> GenericSet<T> assign(GenericSet<T> z) {
    GenericSet<T> g = initialize();
    for(int i=0; i<cardinality(z); i++) {
    Item<T> elem = z.get(i);
    insert(g, elem);
    return g;
    public static <T extends Comparable> boolean subset(GenericSet<T> z1, GenericSet<T> z2) {
    for(int i=0; i<cardinality(z1); i++) {
    Item<T> elem = z1.get(i);
    if(!member(z2, elem))
    return false;
    return true;
    public static <T extends Comparable> int cardinality(GenericSet<T> z){
    Vector v = z.getVector();
    return v.size();
    }

    The issue is not "reference a non-static interface", but simply that you cannot reference a non-static field in a static method - what value of the field ed would the static method use? Seems to me your findEditorData should look something like this:   public static EditorBean findEditorData( String username, EditorBean editorData )
          return editorData.ed.findEditor( username );
       }

Maybe you are looking for