Sorry if this is something small. I have created an abstract class with some child classes. A controller class creates a object of type abstract class of requested child class type and returns the abstract class implemented. The child classes have specific attributes for them. I can't access those attributes of the returned object since this is of the abstract class default type, so i tried casting. But this give an error "item.Default cannot be cast to item.cloth"
How to fix it?
Code:
public class testMain {
public void main(int id) {
Product test;
switch(ProductController.getCategory(id)) {
case "Cloth":
test = (cloth) ProductController.getProduct(id);
break;
case "Wear":
test = (wear) ProductController.getProduct(id);
break;
default:
test = (Default) ProductController.getProduct(id);
}
System.out.println("Product No : " + test.ProductNo);
System.out.println("Title : " + test.Title);
System.out.println("Description : " + test.Desc);
System.out.println("Short Description : " + test.ShortDesc);
System.out.println("Regular Price : " + test.RegularPrice);
System.out.println("Sale Price : " + test.SalePrice);
System.out.println("Category : " + test.Category);
if(((String) test.Category).split(",")[1].contentEquals("cloth")) {
System.out.println("Size : " + ((cloth) test).size);
System.out.println("Age : " + ((cloth) test).age);
}else if(((String) test.Category).split(",")[1].contentEquals("wear")) {
System.out.println("Brand : " + ((wear) test).Brand);
}
}
}
public class ProductController {
private static ProductDB prodDB = new ProductDB();
public static Product getProduct(int prodID) {
Product product;
List<Object> prodTemp = prodDB.getProductDetails(prodID);
String Category[] = ((String) prodTemp.get(6)).split(",");
switch(Category[1]) {
case "Cloth":
product = new cloth(...);
break;
case "Wear":
product = new wear(...);
break;
default:
product = new Default(...);
}
return product;
}
public static String getCategory(int prodID) {
return prodDB.getCategory(prodID).split(",")[1];
}
}
public abstract class Product {
public int ProductNo;
public String Title;
public String Desc;
public String ShortDesc;
public float RegularPrice;
public float SalePrice;
public boolean StockStatus;
public String Category;
public void setRegularPrice(float regularPrice) {
RegularPrice = regularPrice;
setSalePrice(regularPrice);
}
protected abstract void setSalePrice(float regularPrice2);
public float getSalePrice() {
return SalePrice;
}
public void setStockStatus(boolean stockStatus) {
StockStatus = stockStatus;
}
public boolean isInStock() {
return StockStatus;
}
public Product(int productNo2, String title, String desc, String shortDesc, float regularPrice, boolean stock, String Category) {
ProductNo = productNo2;
Title = title;
Desc = desc;
ShortDesc = shortDesc;
setRegularPrice(regularPrice);
StockStatus = stock;
this.Category = Category;
}
public Product(int productNo, String title, String desc, String shortDesc) {
ProductNo = productNo;
Title = title;
Desc = desc;
ShortDesc = shortDesc;
}
public Product(int productNo, String title, String desc, String shortDesc, float regularPrice) {
ProductNo = productNo;
Title = title;
Desc = desc;
ShortDesc = shortDesc;
setRegularPrice(regularPrice);
}
}
public class cloth extends Product{
public String size;
public int age;
public cloth(int productNo, String title, String desc, String shortDesc, float regularPrice, boolean stock, String Category, String size, int age) {
super(productNo, title, desc, shortDesc, regularPrice, stock, Category);
this.size = size;
this.age = age;
}
@Override
protected void setSalePrice(float regularPrice2) {
SalePrice = (float) (regularPrice2 * 0.85);
}
}