The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() method not invoked Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString() method not invoked" Watch "toString() method not invoked" New topic
Author

toString() method not invoked

Srinivas Katta
Ranch Hand

Joined: Feb 01, 2007
Posts: 76
Hi All,

I have executed the below Program

class TSuper{
protected TSuper getCls(){
return new TSuper();
}
String getValue(){
return "In Super";
}
public String toString(){
return "[Super Class]";
}
}

class TSub extends TSuper{
public TSub getCls(){
return new TSub();
}
protected String getValue(){
return "In Sub";
}
public String toString(){
return "[Sub Class]";
}
}

public class TSample{
public static void main(String args[]){
TSuper ts=new TSub();
Object ob=ts.getCls().getValue();
System.out.println(ob.toString());
}
}

and I am expected the output to be printed as " [Sub Class]" as w are calling ob.toString() in SOP statement but teh ouput is printed as "In Sub".

Please explain

Thanks
Srinivas
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9319
    
  17

Srinivas please Use Code Tags when you post a source code. That way your code looks formatted. Unformatted code is hard to read. You can add code tags by wrapping your code in [code] [/code] tags. You can edit your message using button and then add code tags to it.

As for your problem, ob doesn't contain an instance of your TSub class. You assigned ts.getCls().getValue() to ob which is actually the String "In Sub" that your getValue method returns...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Srinivas Katta
Ranch Hand

Joined: Feb 01, 2007
Posts: 76
Thanks for your suggestion, I will Post the Code with Code blocks from here onwards
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Your getValue() method returns a String object, and then you call toString() on that object, so toString() method on the String class will be invoked, if you invoke toString() on your TSub object, then [Sub Class] will be printed. Check the method invocation carefully.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Harshit Sethi
Ranch Hand

Joined: Jul 16, 2010
Posts: 75
Object ob is assigned to a string "In Sub" and by doing so the Object ob now refers to String class whose constructor is initialized with value "In Sub".

The toString() method in Object class is over-ridden by String and Wrapper classes.Thus you can invoke the toString() method ,due to polymorphism the String class' version of toString() method runs which returns the value "In Sub".

I hope that helps you
Moguluri Ravi Kiran
Ranch Hand

Joined: Apr 16, 2010
Posts: 63

Hello Friend,

you are putting "[In Sub]" in Object type & not the TSub type instance,
Hence you are getting so.

to get "[Sub Class]" you have to put TSub type instance in Object type,

place



instead of


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: toString() method not invoked