toString() [Java]

Started by
3 comments, last by Neophyte 12 years ago
I have class A that overwrites toString()

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.
Advertisement
It does not seem possible. How about creating another method, like toString2, and "sometimes" call it instead of toString.

I have class A that overwrites toString()

Correct terminology is overrides.

In general, having two [font=courier new,courier,monospace]toString()[/font] calls is not a good idea. Fact is [font=courier new,courier,monospace]toString()[/font] is not a standard function call, it's automatically called in a variety of cases. So first problem is to figure out what [font=courier new,courier,monospace] toString()[/font] you need in those cases. In case the call might be separated, a [font=courier new,courier,monospace]toCompleteString()[/font] call might work. Or perhaps an[font=courier new,courier,monospace] if(some_static_variable)[/font] might be needed. I'm not sold on those two solutions myself.

Previously "Krohm"


[quote name='Cuajin' timestamp='1332906763' post='4925879']
I have class A that overwrites toString()

Correct terminology is overrides.

In general, having two [font=courier new,courier,monospace]toString()[/font] calls is not a good idea. Fact is [font=courier new,courier,monospace]toString()[/font] is not a standard function call, it's automatically called in a variety of cases. So first problem is to figure out what [font=courier new,courier,monospace] toString()[/font] you need in those cases. In case the call might be separated, a [font=courier new,courier,monospace]toCompleteString()[/font] call might work. Or perhaps an[font=courier new,courier,monospace] if(some_static_variable)[/font] might be needed. I'm not sold on those two solutions myself.
[/quote]
A nicer fix is to create an overloaded method that takes a boolean value that indicates wheter you want to print less, like the following expample


class AObject extends Object
{
public override String toString() { return toString(false); }
public override String toString(bool printLess)
{
if (printLess)
{
return "I am AObject";
}
else
{
return "I am AObject" + super.toString();
}
}
}


This will keep the original toString functionality and on outside conditions you can choose to print less by just passing a bool to the toString function.

PS: Disclaimer: I don't know Java well enough to ensure that code will compile correctly

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

[color=#000088]Use an anonymous inner class, like this


public class Tester
{
public static void main(String [] args)
{
C cObj = new C() {
@Override public String toString() { return "I am C"; }
};
B [] bList = { cObj };
for (int i = 0; i < bList.length; i++)
{
System.out.println( bList.toString() );
}
}
}



[color=#000088]Edit: Fixed whitespace-issues

This topic is closed to new replies.

Advertisement