• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Casting

 
Greenhorn
Posts: 19
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Type mismatch: cannot convert from List<BaseModel> to List<Category> how to do it ?
 
Campbell Ritchie
Marshal
Posts: 79943
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 {


}
 
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>.
 
A feeble attempt to tell you about our stuff that makes us money
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic