3

If say I have some generic class, for example:

public class Attribute<T> {

}

Is it possible now to have specific methods for specific types? I.e. to extend the generic class for certain kinds of types?

For example:

I want to add extra methods to Attribute<String>, but not for say Attribute<Integer>.

3
  • can you give an example of the specific methods your thinking of? It might be that what you really need is Attribute<T,U> with a method like "public U doSomething(T arg)"
    – David
    Jun 14, 2012 at 18:31
  • Something like: Attribute<InputStream> could have a method .readToFile(String filename) returns File...
    – Larry
    Jun 14, 2012 at 18:38
  • it sounds to me like readToFile shouldn't be part of the interface if it isn't used by all implementors. Another option is to go ahead and implement the method for everyone, but throw an UnsupportedOperationException in classes that aren't going to implement the method
    – David
    Jun 14, 2012 at 22:38

4 Answers 4

3

There is no such direct possibility in Java.

But you can have StringAttribute that extends Attribute<String> and adds to it the methods you'd like to. You can make a kind of factory, in a dependency injection fashion, which will construct e.g. Attribute<Integer> for Integer and StringAttribute for String.

2
  • it's worth pointing out, that once the new methods are added to the implementing class, it will no longer be able to pass around the class as an Attribute object since these aren't methods of the interface. If that's important, then it's likely that the inheritance design is incorrect.
    – David
    Jun 14, 2012 at 18:28
  • @David: indeed, the user's code would need to either be not so generic and check for actual type, or ignore the added methods for the generic case. Anyway the need to add something in a special case looks suspicious from design POV.
    – Vlad
    Jun 15, 2012 at 9:25
2

Not directly, but you can do this...

public class StringAttribute extends Attribute<String>
{
    public String myNewMethod() ...
    ....
}
0
1

You can do something like this:

public abstract class Attribute<T> {
    public abstract T getValue();
}

public class Attr1 extends Atrribute<String> {

   @Override 
   public String getValue() {
      return "smth"
   }
}

Create an abstract class which will have the generic type T and abstract method with the same generic type. Then other classes can extend it by specifying real object instead of generic.

1
  • I think what the question is asking is, can an implementation not include all of the interface's api
    – David
    Jun 14, 2012 at 22:39
0

Your class should be abstract:

public abstract class Attribute<T> {
  void someMethod(T object);
}

And then add subclasses such as:

public AttributeString<T extends String> extends Attribute<T>{
    void someMethod(T object){
    }
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.