Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

#ActualNightCreature83

Posted 28 March 2012 - 01:32 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

#5NightCreature83

Posted 28 March 2012 - 01:31 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 : public Object
{
public:
	 virtual String toString() { return toString(false); }
	 virtual 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: I only know C++ so code is in C++ but you should get the idea from it, virtual methods allow overriding in C++

#4NightCreature83

Posted 28 March 2012 - 01:30 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 : public Object
{
public:
	 virtual String toString() { return toString(false); }
	 virtual 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: I only know C++ so code is in C++ but you should get the idea from it, virtual methodes allow overriding in C++

#3NightCreature83

Posted 28 March 2012 - 01:29 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 : public Object
{
public:
	 override String toString() { return toString(false); }
	 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: I only know C++ so code is in C++ but you should get the idea from it

#2NightCreature83

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(Boolean 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.

#1NightCreature83

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

[code=auto:0]
class AObject extends Object
{
public override String toString() { return toString(false); }
public override String toString(Boolean printLess)
{
  if (printLess)
  {
   return "I am AObject";
  }
  else
  {
   return "I am AObject" + super.toString();
  }
}
}
[code=auto:0]

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.

PARTNERS