Your question is a little unclear and lacks all relevant codes. However I'll try to answer it as far as I understand.
What I understand of your goal is: You want to create inherited classes of CandyBox
and finally a CandyBag
class each of which can be represented as string in order to be able to print. Correct me if I get it wrong.
Well, I would suggest you to use Object.toString
by extending the Object
class and overriding the toString
method.
Since I don't know the properties of the inherited classes like Lindt
I just added height
and width
properties to be able to implement this example. So let's get into it...
First we would implement the base class CandyBox
. Normally it should inherit from the Object
class implicitly. But in case of it don't, you should extend it explicitly. In the base class we will override the toString
method to implement our logic so that it can create a string containing its name and its properties.
But note that we declare an abstract method named getName
so that inheritors of this class implement this method for getting their own name. Hence the class itself must be an abstract class as well.
public abstract class CandyBox {
private final double height, width;
public CandyBox(double height, double width) {
this.height = height;
this.width = width;
}
public abstract String getName();
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(getName());
sb.append(" { [height:").append(getHeight()).append("], [width:").append(getWidth()).append("] }");
return sb.toString();
}
}
Now that we have our base class we will create its inheritors Baravelli
, ChocoAmor
and Lindt
respectively.
public class Baravelli extends CandyBox {
public Baravelli(double height, double width) {
super(height, width);
}
@Override
public String getName() {
return getClass().getSimpleName();
}
}
public class ChocoAmor extends CandyBox {
public ChocoAmor(double height, double width) {
super(height, width);
}
@Override
public String getName() {
return getClass().getSimpleName();
}
}
public class ChocoAmor extends CandyBox {
public ChocoAmor(double height, double width) {
super(height, width);
}
@Override
public String getName() {
return getClass().getSimpleName();
}
}
All inherited from the CandyBox
class and its toString
implementation. So these 3 classes know how to generate their string representation.
Let's proceed and create the CandyBag
class which will inherit from the CandyBox
class as well but override the toString
method to implemnt it for its needs.
import java.util.ArrayList;
import java.util.List;
public class CandyBag extends CandyBox {
private final List<CandyBox> candyBoxes = new ArrayList<CandyBox>();
public CandyBag(double height, double width) {
super(height, width);
candyBoxes.add(new Lindt(3.2, 9.2));
candyBoxes.add(new Baravelli(3.3, 10.4));
candyBoxes.add(new ChocoAmor(3.4, 11.6));
}
@Override
public String getName() {
return getClass().getSimpleName();
}
@Override
public String toString() {
String s = super.toString();
StringBuilder sb = new StringBuilder(s);
sb.append("\n").append(getName()).append("'s contents: { \n\t");
for (int i = 0; i < candyBoxes.size(); i++) {
CandyBox cb = candyBoxes.get(i);
sb.append("[").append(cb.toString()).append("]");
String ending = i < candyBoxes.size()-1 ? "\n\t" : "\n";
sb.append(ending);
}
sb.append("]}");
return sb.toString();
}
}
Here it has three fixed candy boxes that created once when it is instantiated (in its consructor method). It also overrode the toString
method because it has to add its candy content to its string representation.
The 99% of the job is done so far. Now it is time to test it. Let's test it in a main class, let's create 1 CandyBag
object and print its string representation.
public class MainClass {
public static void main(String[] args) {
System.out.println("Here we go...");
CandyBag candyBag = new CandyBag(4, 20);
System.out.println(candyBag.toString());
}
}
See the output:
Here we go...
CandyBag { [height:4.0], [width:20.0] }
CandyBag's contents: {
[Lindt { [height:3.2], [width:9.2] }]
[Baravelli { [height:3.3], [width:10.4] }]
[ChocoAmor { [height:3.4], [width:11.6] }]
]}
You can play with the toString
implementation to change the string representation form for your convenience. Go ahead and try this implementation and let me know if you hit your goal or fail for some reason.