A. What is the use of toString() method in Address class ??
B. Why can't i directly put object m1 in print statement in the MailList class ?? The program is not compiling by doing that, error is not able to identify variable m1.
The toString() method is called when you intend to print an object. Since you have overridden the toString() in the Address class it prints nicely the name, street, city.. etc with line breaks.
This method will be called when you print the Address object directly or the List where you have added one or many Address objects.
But you have to provide the List reference correctly I fear your Address List is named ml and you try to print m1
Thanks for the reply John, ya its was my fault putting ml as m1.
i have one more doubt earlier for an instance in the program given below there was no need of overriding toString() method
for displaying linked list. Directly the linked list object is given in the print statement. But for the prior code without overriding
toString() method,
but John how come the output is fine in this code without overriding the toString() method, sorry for asking so many questions but i need to understand the concept
This is the output which i am getting
Original contents of ll: [A, A2, F, B, D, E, C, Z]
Contents of ll after deletion: [A, A2, D, E, C, Z]
shivang sarawagi wrote:without overriding the toString() method,
The Address class printed nice output since it overrides the toString() method. So check what is the class the List is holding... A String class?? So to make the String class print its content nicely what should have been done?? Any guess ?
sorry for asking so many questions but i need to understand the concept
Read the details of Object#toString() and you find that is normal behaviour. There is nothing wrong with it; only, as the link will tell you, you ought to override toString the way you want it to work, in all your classes.