• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method overridden by Class  RSS feed

 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like to know what does it mean when a method is overridden by a class??

Like for example when you append an object value to StringBuilder

Thanks for reading.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Raghav,

Do you have any specific questions? In a simplified way, Overriding occurs when an instance method in the base class is replaced (overridden) by a method with the same name and signature in a class derived from the base. A good place to start your research is the Java Tutorials on Overriding.

Appending a value to a StringBuilder object really has nothing to do with Overriding. When you append a value to an existing StringBuilder object, you are just adding that value to the end of the existing StringBuilder object. Actually, since the StringBuilder class is final, the class cannot be extended - making Overriding the StringBuilder impossible.

Cheers!
Chris
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris R Barrett wrote:

Appending a value to a StringBuilder object really has nothing to do with Overriding.



Are you sure?I got this from Mala Gupta book??
Actually the method append calls the target class’s toString method to retrieve the object’s String representation.If the toString method has been overridden by the class, then the method append adds the String value returned by it to the target StringBuilder object. In the absence of the overridden toString method, the toString method defined in the class Object executes.

Or maybe I am confusing it with something else
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not referring to overriding the toString() in an derived class of StringBuilder. That's referring to how the StringBuilder works. When you append an object to the StringBuilder, the StringBuilder calls the object's toString(). If I create a class and do not create an overriding toString() method in my class that overrides the base class's toString(), then the base class's toString() will be called.

For example:
MyClass has no overriding toString(), therefore it's base class's toString() is called. As you stated "In the absence of the overridden toString method, the toString method defined in the class Object executes.". So, the Object class's toString() is called resulting in my StringBuilder now holding something like:
SB: MyClass@7f31245a

On the other hand, if MyClass overrided the base class's toString() like this:
SB: My Class toString()
In the second example, whatever I specified as the return value of the toString() in my derived class is what StringBuilder appends to the existing StringBuilder object's content.

Note: The annotation @Override is not required to override. Using it is a good idea, though, because if you are not overriding the signature correctly the compiler will tell you.

Get to Chapter 6, Inheritance, in her book and it will all start making sense.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah as I thought something was wrong in my understanding.
I will make sure to check Chapter 6.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!