user326964 user326964 - 5 months ago 26
Java Question

List of generic object s extending abstract class

I have:


  • 1 TableView

  • 1 Observable list I use in that TV

  • 1 abstract baseclass
    called Media

  • 2 subclasses inheriting from Media called Book and CD



What I want to do:

Check if the current element in the list is either CD or Book, if so then set its name to the string put into the field fldName.

Problem:

final ObservableList<Object extends Media> medium = FXCollections.observableArrayList(


It says,


  • Main.Media is a raw type. References to generic type Main.Media should be parameterized

  • Type safety: A generic array of Main.Media> is created for varargs parameter

  • Syntax error on token "extends",, expected

  • Incorrect number of arguments for type ObserableList; it cannot be parameterized with arguments

  • Line breakpoint:Main [line: 35] - start(Stage)



Code:

final TableView<Object> mediaTable = new TableView<>();
final ObservableList<Object extends Media> medium = FXCollections.observableArrayList(
new Book(),
new CD(),
new Book(),
new CD(),
new Book()
);


/*Lots of other stuff in between here*/
/*The first if-statememen checks if any field(fldName) is empty*/

else if(medium.get(mediaTable.getSelectionModel().getSelectedIndex()).getClass()
.getSimpleName() == "CD" ||
medium.get(mediaTable.getSelectionModel().getSelectedIndex())
.getClass().getSimpleName() == "Book"){
String strName = mediaTable.getSelectionModel().getSelectedIndex()).getName();
if(!fldName.getText().isEmpty()){
strName = fldName.getText();
}
}

/*Lots of other stuff below here*/


This one was helpful to some extent but not a full solution:
Calling member-function of generic member

I think this one might have some kinda solution but I don't understand it:
Java - Generic List of Generic Abstract Classes

So how do I solve this?

EDIT:
The definitions of the classes Media, Book & CD,

public abstract class Media<T>{
private String author;
private String name;
private String genre;
private String publisher;
private String mediaType;
private double price;
private int year;
private T length;
//*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*//
public String getAuthor(){
return author;
}
public String getName(){
return name;
}
public String getGenre(){
return genre;
}
public String getPublisher(){
return publisher;
}
public String getMediaType(){
return mediaType;
}
public double getPrice(){
return price;
}
public int getYear(){
return year;
}
public T getLength(){
return length;
}
//*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*//
public void setAuthor(String author){
this.author = author;
}
public void setName(String name){
this.name = name;
}
public void setGenre(String genre){
this.genre = genre;
}
public void setPublisher(String publisher){
this.publisher = publisher;
}
public void setMediaType(String mediaType){
this.mediaType = mediaType;
}
public void setPrice(double price){
this.price = price;
}
public void setYear(int year){
this.year = year;
}
public void setLength(T length){
this.length = length;
}

public Media(String name, String author, String genre, String publisher, String mediaType, double price, int year){

setName(name);
setAuthor(author);
setGenre(genre);
setPublisher(publisher);
setMediaType(mediaType);
setPrice(price);
setYear(year);

}
}

public class Book extends Media<Integer>{
private String coverType;

public String getCoverType(){
return coverType;
}
public void setCoverType(String coverType){
this.coverType = coverType;
}
public Book(String name, String author, String genre, String publisher, String mediaType, double price,
int year, int length, String coverType){
super(name, author, genre, publisher, mediaType, price, year);
setLength(length);
setCoverType(coverType);
}
public Book(){
super("N/A", "N/A", "N/A", "N/A", "N/A", 0.0, 0);
setLength(0);
setCoverType("N/A");
}
}

public class CD extends Media<Double>{
private String type;

public String getCdType(){
return type;
}
public void setCdType(String type){
this.type = type;
}
public CD(String name, String author, String genre, String publisher, String mediaType, double price,
int year, double length, String type){
super(name, author, genre, publisher, mediaType, price, year);
setLength(length);
setCdType(type);
}
public CD(){
super("N/A", "N/A", "N/A", "N/A", "N/A", 0.0, 0);
setLength(0.0);
setCdType("N/A");
}
}


EDIT 2:
Changed from "Object extends Media" to "Media" in both TableView and ObservableList, now these errors come up:


  • Main.Media is a raw type. Referencs to generic type Main.Media should be parameterized

  • Type mismatch: cannot convert from ObservableLIst>> to ObservableList

  • Type Safety: A generic array of Main:Media> is created for varargs parameter



Code:

final TableView<Media> mediaTable = new TableView<>();
final ObservableList<Media> medium = FXCollections.observableArrayList(
new Book(),
new CD(),
new Book(),
new CD(),
new Book()
);


EDIT 3:

public class Main extends Application {

@Override
public void start(Stage primaryStage) {

final TableView<Media<T>> mediaTable = new TableView<>();
final ObservableList<Media<T>> medium = FXCollections.observableArrayList(
new Book(),
new CD(),
new Book(),
new CD(),
new Book()
);

Answer

Changing the type parameter to Media like @sillyfly said was correct, but also you have to convert the items inside as well.

So if the previous version looked like this:

final TableView<Media> mediaTable = new TableView<>();
final ObservableList<Media> medium = FXCollections.observableArrayList(
            new Book(),
            new CD(),
            new Book(),
            new CD(),
            new Book()
        );

The functioning version will look like this:

final TableView<Media<?>> mediaTable = new TableView<>();
final ObservableList<Media<?>> medium = FXCollections.observableArrayList(
            (Media<?>)new Book(),
            (Media<?>)new CD(),
            (Media<?>)new Book(),
            (Media<?>)new CD(),
            (Media<?>)new Book()
        );

And respectively, had the type parameter been Media, then a Book(for instance) would have to be made as (Media)new Book()