• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overriding

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<pre>
class Base
{
String basename;
public Base (String s1)
{
basename = s1;
}
Base()
{

}
/*
public String toString()
{
return basename;
}*/
}
public class Sub extends Base
{
String subname;
public Sub(String s2)
{
subname = s2;
}
public String toString()
{
return subname;
}
}
class Class1
{
public static void main(String arg[])
{
Base b = new Base("Parent");
Base s = new Sub("Child");
System.out.println("basename: " + b.toString());
System.out.println("subname: " + s.toString());
}
}
</pre>
Output:
basename : Base@6 //whatz happening here
subname : Child
If I remove comment form Base output is fine.
basename : Parent
subname : Child
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umesh:
What Prabhu said is absolutely correct.
When the toString method is not overridden,
the Object classes toString() method is
invoked and Sun describes this:
The output will be returned as a String:
getClass().getName() + '@' +
Integer.toHexString(hashCode())

Hope this explaines the issue.
Regds.
- satya
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Prbhu, Satya, Maha Anna
Maha Anna,
I could not see any method summary section in the API. Are ypu asking me to drill down all the classes to find the methods ?.
May be I am missing here something.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
Thats where I got my info from, which I posted
earlier.
I hope Maha Anna was also trying to point to
the same location, if not please check with
him (her ? .
Regds.
- satya
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
maha anna
 
reply
    Bookmark Topic Watch Topic
  • New Topic