2

I have an interface:

public interface InterfaceA {
  public ArrayList<? extends Object> b();
  public Object b(String string);
  public void c(Object object);
}

The first two methods work fine when I implement them:

public class ClassA implements InterfaceA {
   public ArrayList<SomeObject> b() {
      ...
   }

   public SomeObject b(String string) {
      ...
   }
}

But I can't implement the third one like this:

public void c(SomeObject object) {
    ...
}

Is there any way of declare a method with a "generic" parameter to implement it with different objects?

2
  • FYI: public ArrayList<?> b(); will be enough. All Java classes are descendants of Object. Commented May 23, 2015 at 0:46
  • You're right, I just saw it like that and didn't change it, thanks. Commented May 23, 2015 at 0:57

2 Answers 2

2

Yes. It's possible to make a generic method. Something like,

public <T> void c(T object);

It is also possible to make a generic interface. Something like,

public interface InterfaceA<T> {
    public ArrayList<T> b();
    public T b(String string);
    public void c(T object);
}

and then an implementation might look like

public class ClassA<T> implements InterfaceA<T> {
    @Override
    public ArrayList<T> b() {
        // ...
    }

    @Override
    public T b(String string) {
        // ...
    }

    @Override
    public void c(T object) {
        // ...  
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Well, that didn't work, :/ Thanks anyway, I'm going to try with the other answer.
I declare the method in the interface like you said: public <T> void c(T object) { ... } And try to implement it in the class like this: public c(SomeObject object) { ... } And I got an error.
@AndrésMarotta Ahhh... no you implement it with the generic signature too.. but then you can call it with a SomeObject. Or you make the entire class / interface generic. Your question isn't entirely clear.
I will try to explain me better (I'm not a native english speaker). I have some classes that has the same methods, but with different objects. For example, every class makes an insert of an object in the DB, but every class does it with "his" object. Now, I want to declare an interface to "group" those methods and force the classes to implement theme. For that, the interface most be "generic" about the type of objects that are going to past through the classes that implement it. I hope that's more clear.
1

We can't, because if a method parameter is different, it's an overload.

The closest we can do is declare a type parameter on the class:

public interface InterfaceA<T> {
    public void c(T object);
}

public class ClassA implements InterfaceA<SomeObject> {
    @Override
    public void c(SomeObject object) {
        object.foo();
    }
}

class SomeObject {
    void foo() {
        System.out.println(1);
    }
}

But that may or may not be what you're after.

The problem with covariant method parameters (as I understood what you're asking about) is like this:

InterfaceA a = new ClassA();
// passing Object to method which actually
// expects a SomeObject
a.c( new Object() );

Wikipedia has some interesting things to say about this: Covariant method argument type. Apparently Eiffel has it and it's a bit controversial.

3 Comments

I tried that and the only thing that fix is the error. I can do this: public void c(SomeObject object) { ... } And I'm not getting a compile error. But I can't use the "object" like a SomeObject type. I mean, it doesn't get his methods.
You should be able to inside the body of ClassA.c. See my edit.
Oh, it was my mistake. I didn't change the name of the parameter. That works! Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.