3

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 :)

4
  • 1
    i would advise to write the method Customer with lower case: customer Commented May 7, 2019 at 20:05
  • 2
    You need to add the generic type: class TheProduct implements Product<SomeDataSubclass> Commented May 7, 2019 at 20:07
  • Further to @4castle's comment - because T is specifed in both places, if you implement Product<SomeDataSubclass>, the type of plugin must be Plugin<SomeDataSubclass>. Commented May 7, 2019 at 20:08
  • 1
    I can interpret your question two ways - you either want a generic implementation (like class TheProduct<T> implements Product<T>) or you want to supply a type parameter (like class TheSugarProduct implements Product<SugarData>). You'll need to decide which it is, before you go any further. Commented May 7, 2019 at 20:13

2 Answers 2

4

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.

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

Comments

1

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.

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.