• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting  RSS feed

 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public List<Category> listAll() {



return super.listAll(); <------- returns BaseDao list;

}

the error :The return type is incompatible with BaseDao.listAll();

and i've to return list of Category .
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what is the return type of the superclass' method? Remember a List<Foo> is not a subtype of List<Bar> even if class Foo extends Bar.
 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is the super class returns list of Base Model

public List<BaseModel> listAll(
 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Type mismatch: cannot convert from List<BaseModel> to List<Category> how to do it ?
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which case List<Category> is not a subtype of List<BaseModel> You might get away with returning List<? extends BaseModel>, or if might be better to use polymorphism and stick with List<BasModel>. I am not sure which is better.
 
Ayoub Limouni
Greenhorn
Posts: 19
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
package com.news.model;

public class BaseModel {

private Integer id;
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public BaseModel() {

}

public BaseModel(Integer id) {
this.id = id;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String toString() {
return "BaseModel [id=" + id + "]";
}

}
----------------------
package com.news.model;

public class Category extends BaseModel {


}
 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in your Example BaseModel is the super class.
Category is the sub class.
so
in your example you've assigned instance of a super class in to a sub class reference.

Hope this helps.
Supun
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method listAll in your class returns a List<Category>.

The method listAll in the superclass of your class returns a List<BaseModel>.

Your listAll method is overriding the listAll method in the superclass, but the return type is different: List<Category> instead of List<BaseModel>.

That is a problem, becuase the return types are not compatible. In other words, a List<Category> is not a List<BaseModel>, so you cannot make your overridden method return a List<Category>. Note that even though Category extends BaseModel, a List<Category> does not extend List<BaseModel>.

Ways to solve this:

1. Make the listAll method in your superclass return List<? extends BaseModel> instead of List<BaseModel>.

2. Make the listAll method in your subclass return List<BaseModel> instead of List<Category>.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!