Author Topic:   overriding
Umesh
ranch hand
posted April 13, 2000 08:24 AM             

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());
}
}

Output:
basename : Base@6 //whatz happening here
subname : Child

If I remove comment form Base output is fine.
basename : Parent
subname : Child

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 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

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,
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.

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.
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
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
maha anna

|