• Advertisement
Sign in to follow this  

toString() [Java]

This topic is 2157 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
It does not seem possible. How about creating another method, like toString2, and "sometimes" call it instead of toString.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

[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

Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement