Author | Topic: overriding |
Umesh ranch hand |
posted April 13, 2000 08:24 AM
Output: If I remove comment form Base output is fine.
|
Prabhu greenhorn |
posted April 13, 2000 08:40 AM
In my opinion, When the code is commented, In the Base class,you're using the default toString Method from Object class which returns the Class name along with some junk (May be the address .. i'm not sure). But in the Sub class, you're overriding this toString method with your own toString method to display some string. So it's displaying that string. when you uncomment the code, you're overriding the toString method in both the classes, thus displaying both the strings. Hope this helps.
|
satya5 ranch hand |
posted April 13, 2000 10:17 AM
Umesh: What Prabhu said is absolutely correct. When the toString method is not overridden, The output will be returned as a String: getClass().getName() + '@' +
Regds. - satya
|
maha anna bartender |
posted April 13, 2000 10:42 AM
Umesh, What Prabhu and Satya said is is exact reason for your program's output. Nothing more to add I think. Also if you want to know if a class overrides the toString() method or not, just pick up the class API and see in the methods summary, if this toString() is overridden. regds maha anna
|
Umesh ranch hand |
posted April 13, 2000 08:31 PM
Thanx Prbhu, Satya, Maha Anna Maha Anna,
|
satya5 ranch hand |
posted April 13, 2000 09:21 PM
The other day I posted a link to Java 2 API specification. Locate that (using search username satya5) and select the "Object" class (from the lower left frame) and look into the toString method. You should be able to see the details. I hope Maha Anna was also trying to point to Regds. - satya
|
maha anna bartender |
posted April 13, 2000 11:01 PM
Umesh, What I meant was, given a class if you want to know if the class has its own version of toString() (whether it overrides the Object class's toString() version ) you just go to that class in API and see in the method summary if it is there. If it is there means the class overrides. Otherwise if it is found well below the page methods inherited from Object, then it means the class doesn't override that. The above info is extra only. YOu needn't drill down each and every class in API. But atleast for some classes given in SCJP2 objectives from Sun , for example all wrapper classes(Byte/Short/Integer...Double) and String/StringBuffer etc you verify in the API if it overrides or not. regds
|
| | |