0

Is it a valid design to extend an generic class without specifying a type parameter. I don't really care about the type parameter and want to discontinue the generics under my class's hierarchy.

E.g.,: if there is a class A<T,U> where T and U are type parameters.

class B extends A { ... }

Also, is it possible to specify one parameter and skip the other? What will be the syntax? Following did not work:

class C<U> extends A<?,U> { ... }
Asad Iqbal
  • 3,241
  • 4
  • 32
  • 52
  • 2
    Relevant: [What is a raw type and why shouldn't we use it?](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Sotirios Delimanolis Oct 31 '14 at 01:59
  • thanks @SotiriosDelimanolis in my case the hierarchy has become very complex and I don't have enough time to remove the type parameters from top down. Hence trying to reduce its use in my code. – Asad Iqbal Oct 31 '14 at 02:04

1 Answers1

0

Not specifying a type is called a raw type. The problem with it is that it lets you do unsafe stuff.

Here is a quick example:

class A<T> {
    T t;
}

class B extends A {
}

// somewhere
A<String> a = new B();

A warning is raised for the assignment but it is perfectly compilable Java.

Also, is it possible to specify one parameter and skip the other?

No, not really. For your situation I would recommend parameterizing with Object. (If the parameter is declared with an upper bound like <T extends SomeType> use SomeType instead of Object.)

class B extends A<Object, Object> { ... }
class C<U> extends A<Object, U> { ... }

That will behave normally and won't let you do anything insane by accident.

Community
  • 1
  • 1
Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • I should have included this code in the question. But in my case, I am inheriting an interface from this interface: `SuperInterface, K extends ParentInterface> extends SuperSuperInterface` How can I extend my interface from `SuperInterface` above? – Asad Iqbal Oct 31 '14 at 18:48
  • @AsadIqbal That looks like a recursive generic. My best guess is like `class Sub extends SuperInterface`. But I don't know anything about how your project's classes are used. – Radiodef Oct 31 '14 at 18:57