3

I received an error and I have this structure in my program

public interface Shapes<T>{
//methods here
}

public class ShapeAction<T> implements Shapes<T>{
//Methods and implementations here
}

public class Circle extends ShapeAction<T>{
//Some methods here
}

The error is pointing at class Circle extends Shapes<T> where it says "T cannot be resolved to a type". If I set T to string the error will go away but that also means I can only use one datatype. What should I put inside the <> so that I can use any datatype (String, int, double etc) or did I do this the wrong way?

1
  • Where do you use T in Shapes and ShapeAction? Commented Jul 25, 2012 at 17:32

3 Answers 3

6

There are two different concepts. When you write

public class ShapeAction<T>

this means that you are creating a class which, when instantiated, will be parametrized with some class. You don't know at the time which it will be, so you refer to it just as T.

But when you write

public class Circle extends ShapeAction<T>

this means that you want Circle to be a subclass of ShapeAction parametrized with type T. But what is T? Compiler can't tell that: you declared Circle without any type variables.

You have two options. You can make Circle generic too:

public class Circle<T> extends ShapeAction<T>

This way when you make a new instance of Circle you specify what type it works with, and this extends to the superclass.

And what if you want to specify that ShapeAction can be of any type, but without making the subclass generic? Use Object:

public class Circle extends ShapeAction<Object>

This way Circle stays non-generic, but you can use any data types with the superclass.

Sign up to request clarification or add additional context in comments.

Comments

4

Have you tried this?

public class Circle<T> extends ShapeAction<T>{
//Some methods here
}

There is no way the compiler can tell what the T argument means in extends ShapeAction<T> unless you define it first.

Eventually, when you use a reference to a Circle, you will have to provide a type argument for T, which will be cascaded to all other appearances of this type.

Circle<Integer> myInt;

Will cause the compiler to interpret the code as:

public class Circle<Integer> extends ShapeAction<Integer>{
    //Some methods here
}

And so on, for the rest of the class hierarchy.

As such, if you do not define the type T in the class Circle. There wouldn't be a way for the compiler to tell the parameterized type of ShapeAction and this is most likely the source of the problem here.

2 Comments

Thanks that works, but I would just like to ask why do I need to define another <T> in class Circle? I thought that class Circle already extended ShapeAction which has this <T> ?
@dimas It simply does not work that way, the parameterized type is not inherited as you seem to imply.
2

You haven't really shown us enough code to tell what you're trying to achieve, but in general:

If you want to make a method that can accept multiple types of arguments, you want to parameterize the method, not the entire class.

public <T> Shape takeAction(T argument);

Obviously that doesn't really offer much compile time type safety, you'll still probably have to manually resolve the argument to some subset of restricted types at runtime.

1 Comment

Hi thanks for answering, I am just trying to experiment with Generics and polymorphism. But there is not much method I have inserted in those 2 classes and including the interface. I just wanted to check if I can extend the functionalities of Class Circle with that of ShapeAction which has type <T>. I thought that if I do that I can use the methods in ShapeAction which has type <T> where I could explicitly declare what kind of types they should be in Circle Class.

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.