File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes overriding method question and a super question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "overriding method question and a super question." Watch "overriding method question and a super question." New topic
Author

overriding method question and a super question.

ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
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

Joined: Jan 17, 2006
Posts: 1296
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?


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
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?


"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
ken zemaitis
Ranch Hand

Joined: Sep 26, 2005
Posts: 42
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

Joined: Jan 17, 2006
Posts: 1296
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

Joined: Sep 26, 2005
Posts: 42
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

Joined: Jan 17, 2006
Posts: 1296
Check out this tutorial on the super keyword.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: overriding method question and a super question.