Jump to content

  • Log In with Google      Sign In   
  • Create Account


toString() [Java]


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Cuajin   Members   -  Reputation: 155

Like
0Likes
Like

Posted 27 March 2012 - 09:52 PM

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[i].toString() );
		}
   }
}

I want to edit what will be printed. Instead of printing "I am C I am A". I only want to print I am C, but I don't want to edit Class C's toString because sometimes I do want to print both together.

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.
Please help.

Sponsor:

#2 SriLumpa   Members   -  Reputation: 194

Like
0Likes
Like

Posted 28 March 2012 - 12:18 AM

It does not seem possible. How about creating another method, like toString2, and "sometimes" call it instead of toString.

#3 Krohm   Crossbones+   -  Reputation: 2758

Like
0Likes
Like

Posted 28 March 2012 - 12:55 AM

I have class A that overwrites toString()

Correct terminology is overrides.

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

#4 NightCreature83   Crossbones+   -  Reputation: 2500

Like
0Likes
Like

Posted 28 March 2012 - 01:27 AM


I have class A that overwrites toString()

Correct terminology is overrides.

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

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, Mad Max

#5 Neophyte   Members   -  Reputation: 506

Like
0Likes
Like

Posted 28 March 2012 - 04:43 AM

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[i].toString() );
        }
    }
}



Edit: Fixed whitespace-issues




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS