Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding method question and a super question.

 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. I'm trying to code two programs, and I'm having trouble getting them to compile. They are very simple, and for some reason(again) I can't seem to figure them out. Any help would be great. Thanks in advance.


My first program is supposed to display "Duck@memory location - QUACK!" I can code it to display either the "Duck@memory location" or the "QUACK!" portion, but I cannot seem to put the two together. I'm going to show you the code I have for it to display "QUACK!" What I want to know is how to display "Duck@memory location - QUACK!" while only changing the "public String toString();" portion of my code.



This is my second program. It is supposed to display "392105 - Mark Twain" once it has compiled and ran correctly. I am having trouble with two lines of code, and am confused about the "super" statement, and if this program needs/should have one. I have provided lines (______) in the places I am supposed to code things to produce the desired results. Any help is appreciated, once again.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can code it to display either the "Duck@memory location" or the "QUACK!" portion, but I cannot seem to put the two together. I'm going to show you the code I have for it to display "QUACK!" What I want to know is how to display "Duck@memory location - QUACK!" while only changing the "public String toString();" portion of my code.


Some questions to ponder in order to solve this problem:

What do you do to display "Duck@memory location"? How is this related to the overridden toString() method?
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the second ask yourself this:

"Does the parent class need any data from its child(ie does the parent constructor have any arguments?).

If yes, then you need super in the child constructor.

To find out where you put it, ask yourself, what is the first thing that happens in a construcor regardless if it has an explicit parent or not?
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:


Some questions to ponder in order to solve this problem:

What do you do to display "Duck@memory location"? How is this related to the overridden toString() method?


I simply place "myDuck" in the println statement...or "myDuck.toString();".
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ken zemaitis:


I simply place "myDuck" in the println statement...or "myDuck.toString();".

So the next question is: How can you access this "default" behavior in an overridden method?
[ April 10, 2006: Message edited by: Garrett Rowe ]
 
ken zemaitis
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Garrett Rowe:

So the next question is: How can you access this "default" behavior in an overridden method?

[ April 10, 2006: Message edited by: Garrett Rowe ]


I honestly have no clue. I've been trying to figure it out for a while, and I can't even find something remotely similar to it online. If I override it, I get one value, if I don't, I get the other, I cannot figure out how to combine the two.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out this tutorial on the super keyword.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although as I look at it a little more, that tutorial is pretty weak. Basically you use the keyword super to invoke a method in a super class.



If you run this code the output is:
In super class
In sub class

By using the keyword super you can access the overridden method in the super class.
[ April 10, 2006: Message edited by: Garrett Rowe ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic