toString in subClasses Cannot Override abstract toString , Java - java

I have this very long exercise and I came a cross a problem in each sub class.
The problem says and I have no idea what mistake I've made while writing.
If you could check the 4 toString methods I would be much apprecaited .
The code is here: http://paste.org/pastebin/view/39488
I know I should past the code here but it is very long and I'm not able to organize it well.
toString() in Shape cannot override toString() in java.lang.Object; attempting to use incompatible return type
toString() in Square cannot override toString() in java.lang.Object; attempting to use incompatible return type
`
toString() in Sphere cannot override toString() in java.lang.Object; attempting to use incompatible return type
toString() in Cube cannot override toString() in java.lang.Object; attempting to use incompatible return type
thanks

You need to change the return type of the function to String and return the text instead of writing it to System.out.
public String toString() {
return "(" + super.getX() + ", " +
super.getY() +") " + "side: " + super.getDimension1();
}
EDIT: If you want to have a method that outputs the object directly to System.out in textual form, you'll need to call it something else than toString(). This is because toString() is a method belonging to java.lang.Object which all Java classes automatically extend.

toString() has to return String not void.
// false
public abstract void toString();
// right
public abstract String toString();
Note: You should not print (System.out) in the toString() method. You should rather return a String represenation of the object.

because you try to override it with a void return type. toString should return a String.

It should return a string and not void.
public abstract String toString()

toString() is implemented in Object class and every class extends it. This method is in every class and we can't have two method with same signature but different return type.
As toString() is already there with return type String, we can't have one more toString() with any other return type.

Related

Why is it common practice to override the method toString() in java to print the instance variables of the class?

As I've been learning and even some IDEs have it embeded in it, to override the toString() method to print out all instance variables of the class.
The original toString() defined in object.java is defined as follows:
public String toString() {
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
And it's common practice to override it as:
public String toString() {
return "className{" +"var1="+var1+", var2="+var2+'}';
}
Why don't we keep the original functionality of the toString() method and create a new method (with a different name) with this functionality?
We could. But how will other classes, that know absolutely nothing about your subclasses with your myNewToString method, know how to print a string that textually represents, in a concise but informative way, arbitrary subclasses?
The toString method was designed to be overridden to do that. Yes, it does have default behavior but it's not very useful. Its authors wanted you to override it. Overriding it to return what's commonly practiced is more useful, but you don't have to do that. A toString method for an EmailAddress class can return
public String toString() {
return "EmailAddress{localPart = " + localPart + ", domainName = " + domainName + "}";
}
but it's usually more useful to return something like
public String toString() {
return localPart + "#" + domainName;
}
The reason to override toString() is that toString() is called implicit by the compiler every time an object which is not of type String, is added to a string.
So if you have
MyObject o=new MyObject();
C="Hello " + o;
Then the compiler will call o.toString() in order to get a string it can concat to "Hello "
And I should note that it checks if o is null before calling toString() on o. And if o is null, it just generate the string "null"
Opinion: The toString() method should in general and in most cases only be used for debugging (Print/Log) and not as part of the normal program flow.

Method overloading toString()

Hi I was just wondering if it is possible to have multiple toString() methods in the same class. The two different toString() methods print different things. ex:
public String toString(){
return String.format("(%.1f%+.1fi)%n", real, imaginary);
}
public String toString(){
return String.format("z=%.3f(cos(%.3f)+isin(%.3f))%n",real,imaginary,imaginary);
}
No, you can't have two methods with the same name and signature.
A "signature" in this case means the number of arguments and their types. Changing this won't allow you to override toString() twice, it'll just make one a normal method.
public String toString(){
return String.format("(%.1f%+.1fi)%n", real, imaginary);
}
public String toString( boolean fubar ){
return String.format("z=%.3f(cos(%.3f)+isin(%.3f))%n",real,imaginary,imaginary);
}
The second method has a different signature, so it's legal, but doesn't override toString().
You can have a toString() method that takes an argument to indicate the expected format of the output.
Say you have 3 formats. You can have an enum and based on the value you get, you print/return the value in that format.
EDIT 1
Let's say you have an enum
public enum PrintFormat{
F1, F2, F3
}
and the toString() method that you're going to overload
public toString(PrintFormat format){
switch(format){
case F1:
//return in a diff format
case F2:
//return in a diff format
//so on so forth
}
}
No, it is not possible to overrides multiple toString() in the same Class

Override toString method in Java

public class Curiosity {
public void toString()//error because of this specific method name
{
System.out.println("method is successfully implemented");
}
}
How can i use a method of the same name "toString()" if i want to ?
Do I have to give its return type as String if not what should i do to change its return type like suppose if i want to use a void return type for toString does java allow that ?
toString() method must return a String. That's the only way to override Object's toString().
public String toString()
{
return "method is successfully implemented";
}
If you wish to use the same name but not override Object's toString, you can overload the name toString by adding arguments, thus changing the signature of your method.
Example :
public void toString (String something)
{
System.out.println("method is successfully implemented " + something);
}
You are trying to overload toString() method in a wrong manner
Overloaded methods are differentiated by the number and the type of the arguments passed into the method. In the code sample, draw(String s) and draw(int i) are distinct and unique methods because they require different argument types.
You cannot declare more than one method with the same name and the same number and type of arguments, because the compiler cannot tell them apart.
The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.
The only way to use toString() in your class is by keeping the return type as String
public String toString()
{
//your code here
}
That is how it is defined in Objectclass and if you wish to override it you will have to use the exact signature
or if you still wish to use the method name as toString what you can do is change the method's signature.
A method's signature includes method's name and the parameters.
Remember that return type is not a part of a method's signature
You can look at the source code of the java.lang.Object.
The toString method have a return value in String type. You can't have another method which's name is toString but return type is not String.
Actually, it's forbidden in Java in any inheritance relationship. When you call the method, the compiler only cares the name and the parameters. So how can it distinguishes the methods of the same name but with the different return type?

How to make toString() return a complex object?

I need to make a toString() function that returns the string "3 + 2i" for a Complex object Complex(3,2).
This is what Iv done, but I am getting the below error, which I don't know what it means.
As of release 8, 'this' is allowed as a parameter name for the reciever type only, which has to be the first parameter.
public String toString()
{
String s = ("%f + %fi",this.real,this.imaginary);
return s;
}
Can anyone help me out on what I am doing wrong?
Well, it is not Java. In order to format a String, you have to use String.format();
String s = String.format("%f + %fi", this.real, this.imaginary);
You can return what ever String from toString() method. Only thing is you have to override toString() method. But the way you tried is wrong. you can return String value only from toString()
You can try this
String s = String.format("%f + %fi",this.real,this.imaginary);

Override of toString() that makes use of the overridden toString()

Basically this is what I am trying to achieve.
classname#address(?)[original toString()], object's name, object's age
#Override public String toString()
{
return String.format("%s , %s , %d", this.toString(), this.getName(),this.getAge());
}
Problem, toString() gets called recursively.
I can't call super.toString() because that's not what I want. I want 'this' to call the original toString().
This
this.super.toString()
doesn't work for obvious reasons.
I'm running out of steam, can this be done once a method gets overridden? I.e. call the original implementation ?
(my edit) Basically what I need is: override toString to display two properties of 'this' object, and I also want to have 'this' call the original toString.
This is not a project , or work. Just playing(learning) with Java language. Thanks
You're looking for super.toString().
Actually, I was trying to achieve the same thing, where super.toString() wasn't exactly what I wanted. I believe that this is not possible.
Specifically, I have a domain class in Groovy/Grails, and I wanted to override its toString() method to add some extra information:
class Child extends Parent {
public String toString() {
return super.toString(this) + extra_info // not possible!!!
}
}
This is not identical to return super.toString() + extra_info. In one case I get e.g.
com.example.domain.Parent : 15, extra_info. (Wrong. The instance is the same, but toString includes type information.)
In the other:
com.example.domain.Child : 15, extra_info. (Correct, but not possible.)

Categories