• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arguments in methods and super?  RSS feed

 
Sky Wallen
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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!
 
Tony Docherty
Bartender
Posts: 3271
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks guys!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!