Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generics with Generic Parameters and Abstract class

I've got two generic base classes. The second generic class has a constraint on its parameter of the first class.

abstract class FirstClass<T> {...}

abstract class SecondClass<U> where U : FirstClass {...}

This does not work, because FirstClass is not defined. So I need to do this.

abstract class FirstClass<T> {...}

abstract class SecondClass<U, T> where U : FirstClass<T> {...}

Which works. However, this makes implementing these abstract classes ugly.

class SomeClass {...}

class MyFirstClass : FirstClass<SomeClass> {...}

class MySecondClass : SecondClass<MyFirstClass, SomeClass> {...}

This seems redundant to me because I'm specifying SomeClass twice. Is there a way to declare it in such a way that T from FirstClass is automatically the U for SecondClass. What I would really like this to look like is.

class SomeClass {...}

class MyFirstClass : FirstClass<SomeClass> {...}

class MySecondClass : SecondClass<MyFirstClass> {...}

While I doubt this exact scenario is possible, is there a cleaner what to do what I am trying to do?

Edit

Several people have suggested making an IFirstClass interface. But my definitions are closer to this.

class FirstClass<T>
{
    public T MyObj { get; set; }
}

class SecondClass<U, T> where U : FirstClass<T>
{
    U MyFirstClass { get; set; }
}

With an interface I cannot access MyFirstClass.MyObj from SecondClass. While I could create a object T MyObj { get; set; } on IFirstClass, then use new to hide it, silverlight throws a fit in the binding if I do this.

like image 760
cadrell0 Avatar asked Apr 05 '11 17:04

cadrell0


People also ask

What is the difference between abstract class and generic class?

Abstract is used to define something that requires additional definition of functionality before it is considered "complete" (or concrete in Java-certification-test-terms). Generic means it's a class that can handle a wide variety of data types that you define when you instantiate the class.

Can a generic class have multiple generic parameters?

A Generic class can have muliple type parameters.

Is abstract a generic class?

We can first declare an abstract class that uses generics T . Our generic T could refer to any class (i.e. String , Double , Integer , etc.). This is declared when the AbstractJob class is referenced. These generics can be named anything; it doesn't have to be T .

What is the disadvantages of using generics?

Cannot Create Instances of Type Parameters. Cannot Declare Static Fields Whose Types are Type Parameters. Cannot Use Casts or instanceof With Parameterized Types. Cannot Create Arrays of Parameterized Types.


3 Answers

In my experience it is easiest to create non-generic interface to generic classes. It also solves the problem when you need to cast to the base class without knowing the generic type.

interface IFirstClass {...}

abstract class FirstClass<T> : IFirstClass {...}

abstract class SecondClass<T> where T : IFirstClass {...}
like image 74
Stefan Steinegger Avatar answered Sep 25 '22 16:09

Stefan Steinegger


If you are actually using the generic type arguments to FirstClass (as, from your edit, it sounds like you are), then no, what you're looking for is unfortunately not possible. The compiler does not differentiate between type arguments that are related and those that are not.

like image 43
Adam Robinson Avatar answered Sep 22 '22 16:09

Adam Robinson


Create an interface that FirstClass implements. Then you can constrain SecondClass to the interface.

like image 21
topspin Avatar answered Sep 24 '22 16:09

topspin