• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arguments in methods and super?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to understand what the point is in adding arguments to methods. My book gave the following example:

void tauntUser () {
System.out.println("That has gotta hurt!");
}

vs.

void tauntUser (String taunt) {
System.out.println (taunt);
}

For the second one, I guess you would set taunt to "That has gotta hurt!", and then it would produce the same result. So if that's the case, why do we even need to create arguments then?

Also, I'm really confused about when anyone would use "super", especially with associating the constructors? I really just don't understand what associating constructors means and when we would ever need to use it.

I'm slow at this, sorry. Any help would be appreciated. Thanks!
 
Bartender
Posts: 3323
86
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

For the second one, I guess you would set taunt to "That has gotta hurt!", and then it would produce the same result. So if that's the case, why do we even need to create arguments then?


Say you wanted to print out "That has gotta hurt!" and "What a loser!". If you used the first approach you would have to write 2 separate method whereas if you use the second approach you write the one method and call it twice with the 2 different strings. ie:

Also, I'm really confused about when anyone would use "super", especially with associating the constructors? I really just don't understand what associating constructors means and when we would ever need to use it.


Try reading this http://docs.oracle.com/javase/tutorial/java/IandI/super.html
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if you wanted to taunt some with "You call that a sword? Now, this is a sword!" or "Your momma wears Elvin boots!". Without the argument, you'd need to create a new method for each possible taunt.

In this example, the method is simplistic -- but imagine this was in a game like Dragon Age or Dishonored where displaying a message is much more complex that a simple println(). It'd be madness to copy and paste all that code many times, right?

Arguments let you apply the same code to different data -- in this case, what message to display to the player.

As far as super() goes, same deal. If you need to pass information to the constructor of the superclass, calling super() with the arguments lets your subclass "customize" how the superclass constructs itself.

[Edit: Apparently Tony used his Wand of Fast Posting +3 and beat me to the punch.]
 
Sky Wallen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic