So I've been working in a project in which I need to have a list to be filled with its new child class objects.
I've made it with traditional method which one ArrayList will be filled with a type of child Class. But to make the code shorter and more efficient, I'm considering to downcast the ArrayList to have a single ArrayList of parent class that have both of it's child classes object inside it. Is it possible?
These are the parent class of Things
package Model;
public class Things {
protected String id;
protected String name;
protected double price;
protected int stock;
protected int bought;
public Things() {}
public Things(String id, String name, double price, int stock) {
this.id = id;
this.price = price;
this.name = name;
this.stock = stock;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public double getPrice() {
return price;
}
public int getStock() {
return stock;
}
public void minusStock(int bought) {
this.stock = stock - bought;
}
}
and these are its child class of Handphone and Vouchers
Handphone child class
package Model;
public class Handphone extends Things {
private String color;
public Handphone(String id, String name, double price, int stock, String color) {
super(id, name, price, stock);
this.color = color;
}
public String getColor() {
return color;
}
}
Voucher child class
package Model;
public class Voucher extends Things {
private double tax;
public Voucher(String id, String name, double price, int stock, double tax) {
super(id, name, price, stock);
this.tax = tax;
}
public double getTax() {
return tax;
}
public double getsellingPrice() {
return (price + (price*tax));
}
}
Thus to mention the main menu interface will be on different package, I put import Model.*
on it. Will it also be included inside the menu package if I put it that way?