简体   繁体   中英

java classcastexception during interface and some class casting

I am getting ClassCastException while casting between some class say MarketplaceBasicProductList and an interface IMarketplaceProductList classes -why ?

public static IMarketplaceProductList createMarketplaceBasicProductList(Table basicInfoTable) {
        ArrayList list = new ArrayList();
        if (basicInfoTable != null) {
            for (int i = 1; i <= basicInfoTable.getRowCount(); i++) {
                IMarketplaceBasicProduct product = createMarketplaceBasicProduct(basicInfoTable, i);
                list.add(product);
            }
        }
        return (IMarketplaceProductList) new MarketplaceBasicProductList(list);
    }

other details are :

1.

 public interface IMarketplaceProductList extends Serializable {

    public int getProductCount();
    public IMarketplaceBasicProduct getProductMKPInfo(int index);

}
  1. public class MarketplaceBasicProductList extends AbstractProductList { ..............

  2. public abstract class AbstractProductList implements IProductList { .............

do i need to add also implements IMarketplaceProductList here ?

  1. public interface IMarketplaceBasicProduct extends Serializable { ...get methods for MarketplaceBasicProduct functions..

5. public class MarketplaceBasicProduct extends BasicProduct implements IMarketplaceBasicProduct, IGenericProductFields {

  1. public class BasicProduct extends AbstractProduct {

7. public abstract class AbstractProduct extends AbstractGenericProduct implements IProduct {

do i need to add also implements IMarketplaceBasicProduct here ?

You are getting ClassCastException since your class MarketplaceBasicProductList is not IMarketplaceProductList .

MarketplaceBasicProductList extends AbstractProductList implements IMarketplaceProductList 

Looking from the structure that you have, AbstractProductList will be generic for every type of Product list and IMarketplaceProductList will be containing specific methods for MarketplaceBasicProductList , so only MarketplaceBasicProductList will implement IMarketplaceProductList

Also I feel IMarketplaceProductList will be providing features on and above IProductList so it IMarketplaceProductList definition should be like:

public interface IMarketplaceProductList extends IProductList {

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM