answersLogoWhite

0


Best Answer

By default every class you write inherently extends the base Object class. This has a basic toString() method which merely returns the name of the class followed by a hex representation of the hash value. By overriding this method, you can return a more meaningful value specific to your class, such as member attributes and their values.

User Avatar

Wiki User

15y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Why do you need to override the toString method?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What does the ToString-method accomplish?

The ToString method accomplishes a very satisfying end result. Using the ToString method, one will be able to textually represent an object by only using strings.


Does Every class has a toString method and an equals method inherited from the Object class?

Yes - in Java, every class has this method, which is inherited from the Object class. Often, the inherited method does nothing particularly useful, but you can override it with your own implementation.


What is toString and when you use it?

The toString method is used to create a string representation of the given object. This is often used to give diagnostic output to the programmer.


How do you convert numbers into words using java?

Using toString() method


Can you override an instance method and make it final?

No. Once a method is declared final in a class, no derivative of that class can override that method.


What method that returns string representation of the contents of the variable?

Since the question is in the Java category: in Java, the method is called toString(). This method will automatically be invoked if you implicitly convert an object to String type, for example: "The answer is: " + myObject In this example, the String concatenation (the plus sign) forces the object, myObject, to type String - to do this, the object's toString() method will be called.


Can you override the life cycle methods of a JSP?

You cannot override the jspService() method but you can override the jspInit() and jspDestroy() methods


Why can't method override be used without inheritance?

Overriding a method means that you are replacing an existing or virtual method that has already been defined in the parent object class, so without using inheritance, there can be no existing method to override.


Any method in a super class can be overridden in its subclass?

False.Any method declared as final cannot be overridden by any subclasses.You also cannot technically override a private method. While your subclass can have a method with the same definition as a private method in the superclass, it does not actually override that method.


Why must you override the hashcode method while overriding the equals method?

Because, if two objects are supposed to be equal as per the equals() method, then the value returned by the hashCode() method must also be the same. This will not be the case if you override only the equals method and this can have some confusing effects when using those objects with hash related collections. So it is always a good idea to override the hashCode() method if you are providing an implementation for the equals method.


Why do you need to create an object reference variable to call methods cant you directly use the objects that is created?

Creating an object and invoking a method of that object are 2 distinct steps. From good programming practice, it is better off to have 2 separate statements at least to do this. For example: (new object()).ToString(); // creates an instance and invoke ToString() vs. object objRefence = new object(); objRefence.ToString(); The variable objReference is needed to invoke the method ToString(), because the object refereced of the one being created from the new operator needed to be cached somewhere. There is no major differences between the 2 above. However, if there is another method to be invoked, the second way (use variable reference) is the only way, the first one cannot. (You can replace ToString() with GetHashCode(), and prints it on the console twice, the first one may produce 2 different values, while the second one will be the same value. Think of GetHashCode() returns the object id in a way)


What method do you override to initialize instance variables in a JSP declaration?

jsp init()