0

Is there a way to declare a generic class that extends from its parameter? I want to have a legal equivalent for what I'm trying to do here:

public class Box < T > extends  T{

}

Thank you in advance.

1
  • Ok here's the context of my program. I'm designing a Class library for mathematical objects, there are classes to represent sums, products, complex and real numbers, etc. I'll like to have a container class called Constant in order to treat larger expresions as if they were complex numbers and use them as coeficients. The container class should have the same behavior as its content, so that is why I'm trying to do this. Commented Dec 7, 2013 at 21:47

2 Answers 2

1

What are you trying to do? Please explain it in more words, because your current code (and explanation) makes no sense, perhaps it is understandable after all once we know the details.

Take your example as example, with type parameter T := Integer.

Then you want to do the following:
Box<Integer> extends Integer

Why would you want that Box<Integer> is a subclass of Integer?
What do you gain with having that behaviour accomplished.

What you are asking is simply not possible in any (OOP) programming language and/or reasoning, and I do not see the need fo rit.

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

1 Comment

It is possible in C++. And is usable in certain cases.
1

Generics are compile-time instrument, they do not exist at runtime. So the direct answer to your question is "no".

Comments

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.