Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

overriding method question and a super question.

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic