permaculture playing cards*
The moose likes Java in General and the fly likes what is need of super() in the foolowing line of code? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "what is need of super() in the foolowing line of code?" Watch "what is need of super() in the foolowing line of code?" New topic
Author

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

madhav changala
Ranch Hand

Joined: Dec 20, 2005
Posts: 57

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 ]

saivenumadhav
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
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.


ASCII silly question, Get a silly ANSI.
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
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

Joined: Mar 26, 2002
Posts: 442
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

Joined: Aug 16, 2005
Posts: 14190
    
  20

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Stuart Ash
Ranch Hand

Joined: Oct 07, 2005
Posts: 637
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

Joined: Oct 07, 2005
Posts: 637
Ah heck!! He meant super() and not super.toString(). We got misled.
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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...


"I'm not back." - Bill Harding, Twister
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Dec 20, 2005
Posts: 57
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
 
GeeCON Prague 2014
 
subject: what is need of super() in the foolowing line of code?