Results 1 to 8 of 8
Thread: overriding ToString()
- 08-04-2011, 04:33 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
overriding ToString()
Can someone explain to me how to override the toString(method) in an Exception class? My instructor tried explaining it to me but it kind of went above my head. Sample code below.
Java Code:public class MetalR0'sException extends Exception { public MetalR0'sException(String message) { super(message); } public MetalR0'sException() { super("This message should print when exception is thrown"); }
- 08-04-2011, 04:43 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Exactly the same as you would override toString in any other class, since an exception is just a class.
- 08-04-2011, 04:47 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
So I would just add a public String to String method to the above code and set the return String equal to the message I want the error to have? ie
public String toString() {
String s = "This message should print ....."
return s;
}
Right?Last edited by MetalR0; 08-04-2011 at 04:49 AM.
- 08-04-2011, 04:51 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Yup. You could also just do
Java Code:return "the message";
- 08-04-2011, 04:57 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
Got it. Thanks!
- 08-04-2011, 06:24 AM #6
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
I'd like to add that any time you override any method you should use the override annotation. So toString will look like this:
This same @Override annotation can be used anywhere and it will allow you to get compile time errors when you improperly override a method, without this method you may just get strange behavior, take the following as an exampleJava Code:@Override public String toString(){ return ""; }
This will compile but when you try printing an object you get something likeJava Code:public String toStrng(){ return ""; }
Class@152af7
Which is the default toString, with the override annotation, an error will occur signaling your missed i
- 08-04-2011, 09:48 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,466
- Rep Power
- 16
Why do you need to override the toString() method?
At least for that example above, the existing one (on Throwable) already supplies the message text.
And that class name is illegal.
- 08-04-2011, 10:09 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Confused about Overriding - Please Help
By jazzermonty in forum New To JavaReplies: 15Last Post: 01-24-2011, 05:20 PM -
overriding v/s hiding
By parminder in forum New To JavaReplies: 3Last Post: 01-17-2011, 04:24 AM -
Overriding
By prasanna1157 in forum New To JavaReplies: 7Last Post: 09-07-2010, 07:47 AM -
overriding toString method
By matin1234 in forum New To JavaReplies: 3Last Post: 06-01-2010, 04:35 AM -
Overriding
By renuka_renukut in forum Advanced JavaReplies: 3Last Post: 05-21-2010, 08:45 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks