-2

I have two packages and both packages have Excel classes and also Invoices and Packing classes, I would like to create one Excel where i could extend from ,each excel need to know about Invoice and Packing Lists

public class Excel {

   protected final List<? extends T> invoices;
   protected final List<? extends T> packing;

   public Excel(List<? extends T> invoices, List<? extends T> packing) {
      this.invoices = invoices;
      this.packing = packing;
   }
}

I have extended that Excel in another Excel class and in the constructor super(invoices,packing) it says required type List ? extends T provided List Invoices List Packing

public Excel(List<Invoice> invoices, List<PackingList> packingLists) {
    super(invoices,packingLists);
Bilal Shah
  • 1,135
  • 7
  • 17
  • 42
  • 4
    Is `T` a generic type parameter? If it is, where is it declared? – Eran Jan 14 '20 at 12:09
  • @Eran i have created fields thinking that List could taky anything he like and then when extending that class just passing that lists of specific classes – Artjom Prozorov Jan 14 '20 at 12:10
  • 2
    What is `T`? If it's a generic type parameter, it should be declared either in the class level or in any relevant method. If it's not, this means you must have some class or interface named `T`. – Eran Jan 14 '20 at 12:12
  • Does this answer your question? [Instantiating a generic class in Java](https://stackoverflow.com/questions/1090458/instantiating-a-generic-class-in-java) – Philippe B. Jan 14 '20 at 12:57

2 Answers2

2

by addin on top of your class

 public class Excel <T>{

   protected final List<? extends T> invoices;
   protected final List<? extends T> packing;

   public Excel(List<? extends T> invoices, List<? extends T> packing) {
      this.invoices = invoices;
      this.packing = packing;
   }
}

and now when you want to call it you can just extend it like this

public class Something extends Excel <YourClass>
Karim
  • 1,004
  • 8
  • 18
1

As Eran pointed out. If you want to use generic parameters you need to declare them somewhere. Here you don't declare T so you cannot use it.

With the following code :

public class Excel<T> {

// ...

}

When you create a new Excel you will be able to specify the type you want :

Excel<MyType> excel = new Excel<>();

You could also implement a generic interface.

Philippe B.
  • 485
  • 4
  • 18