public class A
{
public String toString()
{
return "I am A";
}
}
I have class B that overwrites toString() but from class A (really weird).
public abstract class B
{
private A aObject;
public B()
{
aObject = new A()
}
public String toString()
{
return aObject.toString() ;
}
}
Then class C
public class C extends B
{
public String toString()
{
return "I am C " + super.toString();
}
}
In the tester class I have
public class Tester
{
public static void main(String [] args)
{
C cObj = new C();
B [] bList = { cObj };
for (int i = 0; i < bList.length; i++)
{
System.out.println( bList.toString() );
}
}
}
I want to edit what will be printed. Instead of printing "[color=#008000]I am C[color=#FF0000] I am A". I only want to print [color=#008000]I am C, [color=#000000]but I don't want to edit Class C's toString because sometimes I do want to print both together.
[color=#000000]Is there a way to downcast or upcast in main to choose what to print from the toString? Or any other way for that matter.
[color=#000000]Please help.