Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Inheritance help
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Raknarg




PostPosted: Tue Feb 04, 2014 12:50 pm   Post subject: Inheritance help

I have this question on one of my assignments:

. Adjust the toString() method in the Fish class so that it displays the proper type of fish. You
cannot use instanceof nor use any IF statements.

(e.g., "A 16cm 0.2kg Perch" instead of "A 16cm 0.2kg Fish" )

Basically it's an assignment on inheritance and interfaces. We have a fish class, and that will be inherited by a bunch of specific fish classes. I have no idea how to accomplish what's asked above without using what it prohibits, especially since it's asking for me to make this method in the super class and not in the subclasses.

Any ideas?

EDIT: My prof said that whenever he gives us an assignment, he gives us everything we need so I shouldn't have to make more instance variables or add anything to the parameters
Sponsor
Sponsor
Sponsor
sponsor
DemonWasp




PostPosted: Tue Feb 04, 2014 2:13 pm   Post subject: RE:Inheritance help

You could add a method like getFishType() that returns a string like "Perch" or "Mackarel" (etc), which you can then call instead of using the "Fish" string.

If your base class is Fish (and, presumably, is abstract) then your base class should declare getFishType as abstract. Then, subclasses will have to implement it to return the correct string for that particular instance.
Raknarg




PostPosted: Tue Feb 04, 2014 4:31 pm   Post subject: RE:Inheritance help

Oh that's brilliant, but I'm not sure if I'm allowed to add methods into the class. I'll do it anyways if I can't find another way.
Tony




PostPosted: Tue Feb 04, 2014 5:56 pm   Post subject: Re: Inheritance help

Raknarg @ Tue Feb 04, 2014 12:50 pm wrote:
(e.g., "A 16cm 0.2kg Perch" instead of "A 16cm 0.2kg Fish" )

How does an instance know that it weights 0.2kg but doesn't know that it's also a Perch? You don't need a new method, just a Perch version of toString().
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Raknarg




PostPosted: Tue Feb 04, 2014 6:04 pm   Post subject: RE:Inheritance help

No I get that, that's why the question is stupid.

There's a fish class, and some other fish classes which inherit from this fish. However within the fish class we are supposed to tell the toString function what kind of fish it is. I think it's a stupid way to do it, but that's one of the requirements I guess.
Zren




PostPosted: Tue Feb 04, 2014 7:31 pm   Post subject: Re: Inheritance help

Tony @ Tue Feb 04, 2014 5:56 pm wrote:
Raknarg @ Tue Feb 04, 2014 12:50 pm wrote:
(e.g., "A 16cm 0.2kg Perch" instead of "A 16cm 0.2kg Fish" )

How does an instance know that it weights 0.2kg but doesn't know that it's also a Perch? You don't need a new method, just a Perch version of toString().


Quote:
to make this method in the super class and not in the subclasses.


Doesn't really sound like he's allowed have a Perch.toString() overloading the Fish.toString() though.

If you're not allowed to have an abstract Fish.getFishType(), nor allowed to overload Fish.toString() with a Perch.toString(), and if the subclass is called Perch, you could always do this.getClass().getName(). Though that's a silly way to do that unless the only reason you're using the toString() is for logging output, where you'd actually want to know what the class's name + property values are.
Raknarg




PostPosted: Tue Feb 04, 2014 8:05 pm   Post subject: RE:Inheritance help

Yeah, I have a feeling he doesn't mean what he says. However I decided I will just go with what DemonWasp suggested, by using an abstract method and override it in the subclasses.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: