Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly implement a generic interface in Java

Tags:

java

oop

generics

I apologize if this comes off as someone simply turning to stack overflow or if it sounds too easy. I've been stuck on this problem all day and all the resources I've tried (google, stack-overflow, friends and Oracle Java books haven't proved helpful.

So i was given the following section of code I have to work on.

import org.Plugin

public interface Product<T extends Data>{
   void Customer(Plugin<T> plugin);
}

I've managed to break down the base of what this code does, concerning the Product class I know that:
- T extends Data : Bounded type parameter for T, T has to be a subclass of Data
- I need to build a class that implements the Product class along the the Customer method.

So far I came up with this:

class TheProduct implements Product{
   @Override
   public void Customer(Plugin plugin){
   }
}

What i'm confused about, is what the (Plugin<T> plugin) part of the Consumer method does. Does Plugin refer to a parameter such as a variable? If so, do I need to include it in TheProduct class?

I feel like i'm missing something major but have no idea what.

Thank you to everyone who took the time to read :)

like image 869
Patrick Avatar asked Sep 23 '26 13:09

Patrick


2 Answers

Both Plugin<T> and Product<T> are examples of generic types. The fact that you you've used the T parameter in both places, means that you expect these both to receive the same generic type.

For example, if you specified string as the type for T (although this wouldn't meet your data constraint), your customer method would expect to receive an instance of Plugin<string>.

In your case, you also have a constraint on T, that it must be some type that implements Data, but the same logic applies: T is a placeholder for your generic type.

The name T is arbitrary. For types that expect a single generic parameter, the convention is to call it T, but you can use something more descriptive, if it helps.

like image 198
richzilla Avatar answered Sep 25 '26 01:09

richzilla


Bounded type parameter for T, T has to be a subclass of Data

or Data itself.

What the (Plugin<T> plugin) part of the Consumer method does?

It expects a generalised Plugin<T>, not a raw Plugin. An example of Plugin<T> would be either Plugin<Data> or Plugin<SubData> where SubData is a subclass of Data.

Does Plugin refer to a parameter such as a variable?

Plugin can't refer. You can. In the definition of Customer, you refer to that type parameter T defined in Product<T extends Data>.

If so, do I need to include it in TheProduct class?

Yes, you do. You've got one type parameter T which generalises the interface Product. Now you are moving from abstraction to implementation. T is an abstract thing. You need something real, a real class which extends Data.

class Data {}
class SubData extends Data {}

interface Product<T extends Data>{
  void Customer(Plugin<T> plugin);
}

class TheProduct implements Product<SubData>{
  @Override
  public void Customer(Plugin<SubData> plugin){
  }
}

Customer is a very strange name for a method because it's a noun and it's capitalised. We would write registerCustomer, serveCustomer, sellPluginToCustomer. You get the idea.

like image 31
Andrew Tobilko Avatar answered Sep 25 '26 01:09

Andrew Tobilko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!