• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

toString() method not invoked

 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9519
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Srinivas Katta
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your suggestion, I will Post the Code with Code blocks from here onwards
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Harshit Sethi
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic