Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is need of super() in the foolowing line of code?

 
madhav changala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

here
super() is it really necessary? i have tried without it , and it works fine,
thanks in advance
[ January 09, 2006: Message edited by: Jim Yingst ]
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's invoking the base class toString(). Without it, it would invoke the current (or lowermost-in-the-hierarchy) toString, and you run the risk of getting into an infinite recursive loop. I am surprised that didn't happen to you already.
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not needed in this case, but if the super class implemented toString you'd be interested in it's returned value as well. Wacking it here wouldn't make any difference though, as you discovered.
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stuart Ash:
It's invoking the base class toString(). Without it, it would invoke the current (or lowermost-in-the-hierarchy) toString, and you run the risk of getting into an infinite recursive loop. I am surprised that didn't happen to you already.


Given that it works I think he meant the call super.toString(), not just wacking super from it. I could be wrong on that assumption
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15644
47
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The call super(); in the constructor is totally useless. It calls the no-args constructor of the superclass.

This happens automatically, so the call doesn't do anything at all. You can remove it and the program will still run exactly as before.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ta Ri Ki Sun:


Given that it works I think he meant the call super.toString(), not just wacking super from it. I could be wrong on that assumption


He said he tried without it and it works. Guess we should for him to reply.
 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah heck!! He meant super() and not super.toString(). We got misled.
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stuart Ash:
Ah heck!! He meant super() and not super.toString(). We got misled.




I wish the line of code could be marked in bold or italics or something to clear it up. sigh, whatever he meant though, he'll have all 3 possible answers now
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If he whacked the "super" in "super.toString()", the program would still run just fine, because (on cursory examination, I could have missed something) SmallBall.toString() is never called. If that method were ever called, then there would indeed be a problem. But as is, he could also have just erased the whole toString() method and again changed nothing.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Stuart]: Ah heck!! He meant super() and not super.toString(). We got misled.

What? He clearly said super(). Who misled you?

Adding to Jesper's answer, I would note that although calling super() is unnecessary (since it's done impliticly anyway), some people like to always make this call explicitly. It can be useful for reminding newbies that the super constructor is getting called. Although the down side is it may confuse newbies, as they may think that if the super() isn't there, it isn't called. Anyway, some people think requiring an explicit super() is good idea, and some IDEs such as IntelliJ can be configured to either (a) warn you if you fail to call super(), or (b) warn you if you do call super() when it's not necessary. Whichever you prefer...
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While the call is unnecessary, declaring it useless is a matter of opinion not fact. I prefer to make the call explicitly for consistency and readability. Plus, if someone is so new they don't understand this is otherwise implicit hopefully it will force them to find out. I'll take that over them continuing on oblivious to what's happening for them. Besides, within reason the more clearly code expresses what it's doing and the less it leaves to be implied the better.

I wouldn't object to other's leaving it implicit, but it's not my preference.
 
madhav changala
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all .But only few people got right track , i donot y someone misled by super().to String
any how i am thankful to u r advice
expecially to Jim Yingst .
ok
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by madhav changala:
Thanks to all .But only few people got right track , i donot y someone misled by super().to String
any how i am thankful to u r advice
expecially to Jim Yingst .
ok


In the future, you should only post the code relevant to your question. I think that the large amount of code you posted here lead to the confusion. In this case, you could have simply posted the constructor, and perhaps the enclosing class as well.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic