3

I had an interface initially as below.

public interface testMe {
 public Set<String> doSomething();
}
public class A implements testMe {
 public Set<String> doSomething() {
 return // Set<String>
 }
}

I had similar classes implementing testMe. Now I have to add one more class which returns Set<Some Object>

public class X implements testMe() {
 public Set<Some OBject> doSomething() {
 }
}

How could i add this method in the interface without breaking existing classes?

Josh Crozier
242k56 gold badges401 silver badges316 bronze badges
asked Jun 2, 2010 at 1:06

3 Answers 3

5

You can use

public interface testMe {
 public Set<?> doSomething();
}

Or

public interface testMe {
 public Set<? extends CommonSuperclass> doSomething();
}
answered Jun 2, 2010 at 1:16
Sign up to request clarification or add additional context in comments.

Comments

2

You can't for two reasons.

  1. A class or interface can't have two or more methods that have the same number and type of parameters with the same name but differing return types; and

  2. Because of type erasure, all Set<...> instances are, at runtime, simply Set, so they would have the exact same return type anyway.

You will need to name the second something different.

The more complicated answer is that you can make the parameter type extensible:

public interface TestMe<T extends Serializable> {
 Set<T> doSomething();
}
public class A implements TestMe<String> {
 @Override
 public Set<String> doSomething() { ... }
}
public class X implements TestMe<ASerializableObject> {
 @Override
 public Set<ASerializableObject> doSomething() { ... }
}
answered Jun 2, 2010 at 1:15

Comments

2

I don't believe you can, because type erasure will ruin the effect you have in mind.

You can parameterize the interface:

import java.util.Set;
public interface ISomething<T>
{
 Set<T> doSomething(T [] data);
}

And the implementation:

import java.util.HashSet;
import java.util.Set;
public class Something<T> implements ISomething<T>
{
 public static void main(String[] args)
 {
 Something<String> something = new Something<String>();
 Set<String> set = something.doSomething(args);
 System.out.println(set);
 }
 public Set<T> doSomething(T [] data)
 {
 Set<T> foo = new HashSet<T>();
 for (T x : data)
 {
 foo.add(x);
 }
 return foo;
 }
}

I'm not sure this accomplishes what you have in mind, though.

answered Jun 2, 2010 at 1:17

1 Comment

The problem here is, I do not instantiate the concrete classes directly but encapsulated in factory method. How would one write factory method around it returning ISomething<String> as well as ISomething<Some Object>. If we parameterize even the factory, then the problem is moved to the consumer of the method. It has to decide what is returned.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.