Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java interface design

Tags:

java

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?

like image 429
Nayn Avatar asked Jan 29 '26 08:01

Nayn


1 Answers

You can use

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

Or

public interface testMe {
   public Set<? extends CommonSuperclass> doSomething();
}
like image 85
Chris Lercher Avatar answered Jan 30 '26 22:01

Chris Lercher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!