OOP can get confusing

in Java all classes derive/inherit one class. this is the object class. one of the methods in the object class is the toString() method this method converts the object to a string (a string is like a sentence). because all classes inherit object they all have the toString() method.

now here is the funny part. there is a Java class String(see above if you don't know what a string is) because it is a java class it inherits object. so you can call the toString() method on a string. In other words you can convert a string to a string

that's OOP for you
So according to you, f(x)=2x should be undefined at x=0, because since its output is the same as its input there, what's the point of defining it?
Indeed. The design would only be "funny" if String didn't have a toString method.
When the toString() method says that any object an be converted to string, then why not that any object be string itself. I agree with hamsterman
closed account (o3hC5Di1)
Now now gentlemen, let's not come down too harsh on Mr. Parsons - I'm sure we could all see how one would find it odd at first that a String class has a toString() member function without knowing the underlying reason. :)

All the best,
NwN
closed account (1yR4jE8b)
A String in Java isn't a primitive type, so System.out.println(stringVar) calls the toString() method. If the class didn't have it...that would be brain dead.
Topic archived. No new replies allowed.