Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Benefits of Using Generics in a Base Class that Also Implement the Same Class

Tags:

java

generics

I recently ran across this scenario in code that I didn't write and while there may be some design benefit to this approach, I can't seem to squeeze this rationale out of my own brain. So before I go and look foolish, I'm hoping for some feedback here.

Service interface something like this:

public interface Service {...}

Then, a base class that adds a generic reference to the Service interface where T extends the Service, but then the overall base class also implements the interface. Something like this:

public class ServiceBase<T extends Service> implements Service {...}

Why would you do this? I'm noticing that in practice the extension of ServiceBase always uses the same class name as T as the one that is being declared; so there's not really any magic polymorphic benefit here. Something like this:

public class MyService extends ServiceBase<MyService> {...}

and, the MyService class is never a container for the generic (e.g., I don't believe this is signaling some kind of self-containing list, where MyService could contain a list of MyServices).

Any ideas/thoughts on why someone would do this?

like image 791
Eric Hendrickson Avatar asked Jan 01 '17 21:01

Eric Hendrickson


People also ask

What are the advantages of using generics?

Generics shift the burden of type safety from you to the compiler. There is no need to write code to test for the correct data type because it is enforced at compile time. The need for type casting and the possibility of run-time errors are reduced. Better performance.

What are the benefits of using generic types in a class definition?

In a nutshell, generics enable types (classes and interfaces) to be parameters when defining classes, interfaces and methods. Much like the more familiar formal parameters used in method declarations, type parameters provide a way for you to re-use the same code with different inputs.

What is the purpose of generic class and generic method?

Generic methods are methods that introduce their own type parameters. This is similar to declaring a generic type, but the type parameter's scope is limited to the method where it is declared. Static and non-static generic methods are allowed, as well as generic class constructors.

What is generics in C #? What are the benefits of generics?

Generics is a technique that improves your programs in many ways such as: It helps you in code reuse, performance and type safety. You can create your own generic classes, methods, interfaces and delegates. You can create generic collection classes.

Why do we need generic classes in Java?

After you define a generic class, you can make any number of objects that have similar behaviors but that may work with different data types. You can make generic structures, interfaces, methods and delegates.” It helps you to maximize code reuse, type safety, and performance. You can create generic collection classes.

What is a generic class in C++?

Generic classes have type parameters. Separate classes, each with a different field type in them, can be replaced with a single generic class. The generic class introduces a type parameter. This becomes part of the class definition itself.

How many types of parameters can a generic class have?

The type parameter section of a generic class can have one or more type parameters separated by commas. We can write a single generic method declaration that can be called with arguments of different types. Based on the types of arguments passed to the generic method, the compiler handles each method call appropriately.

What is the purpose of generics?

The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types. An entity such as class, interface, or method that operates on a parameterized type is a generic entity. Why Generics?


1 Answers

Why would you do this? I'm noticing that in practice the extension of ServiceBase always uses the same class name as T as the one that is being declared; so there's not really any magic polymorphic benefit here.

Generics don't exist to create magic polymorphim. It is mainly a way to add constraints on types at compile time in order to reduce clumsy cast and error type at runtime.

In your case, suppose that ServiceBase class is abstract and has a process() method which needs to create at each call a new instance of the concrete class we declare in the parameterized type.
We call this abstract method createService().

Without using generics, we could declare the method like that public abstract ServiceBase createService().

ServiceBase without generics

public abstract class ServiceBase implements Service {

    public abstract ServiceBase createService();

    @Override
    public void process() {
         createService().process();
    }

}

With this declaration, the concrete class may return any instance of ServiceBase.

For example, the following child class will compile because we are not forced to change the returned type of createService() to the current declared type.

MyService without generics

public class MyService extends ServiceBase {

    @Override
    public ServiceBase createService() {    
        return new AnotherService();
    }

}

But if I use generics in base class :

ServiceBase with generics

public abstract class ServiceBase<T extends Service> implements Service {

    public abstract T createService();

    @Override
    public void process() {
         createService().process();
    }

}

The concrete class has no choice, it is forced to change the returned type of createService() with the parameterized type declared.

MyService with generics

public class MyService extends ServiceBase<MyService> {

    @Override
    public MyService createService() {  
        return new MyService();
    }

}
like image 105
davidxxx Avatar answered Sep 20 '22 16:09

davidxxx