Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-01-2008, 10:52 PM
Member
 
Join Date: Jan 2008
Posts: 5
Kreuz14 is on a distinguished road
Inheritance, methods, and toString...
Code:
Vehicle [ ] acme = new Vehicle [5]; acme[0] = new LightTruck(111, "Elmer Fudd", "GM", 500); acme[1] = new Car(222, "Wile E Coyote", "Ferrari", 2, 10); acme[2] = new Passenger(333, "Foghorn Leghorn", "Toyota", 5); acme[3] = new Commercial(444, "Daffy Duck", "Mack", 42000); acme[4] = new Vehicle(555, "The Roadrunner", "Honda"); for(int i = 0; i < acme.length; i++) { System.out.println(acme[i].toString() + "\n"); } acme[0].changeCapacity(250); //ERRORS HERE
In the above code, LightTruck/Car/Etc extends Vehicle.
All the classes have their own toString method which print different things.
LightTruck has a method called changeCapacity.

The loop works and all the different toString methods return different values according to how its defined in the subclass (including private instance variables from the subclass).

But why does changeCapacity not work and errors? The error log seems to suggest that it is because the compiler checks the Vehicle class for the changeCapacity method, but if thats true, then how come toString works? Wouldn't toString() simply return what is only in the Vehicle method?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-02-2008, 12:12 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
The object located at acme[0] is a reference to a LightTruck object, but you are actually calling changeCapacity(int) on a Vehicle object. Where is changeCapacity(int) defined? If it's not in the Vehicle class, you may want to put it there and try again.

To answer your question about toString(), think of it as a universally assigned method to all objects/classes. Every object gets a specific set of methods defined here. From that link, you can see that toString() is defined there. If you've defined toString() methods in your Vehicle subclasses, then you've essentially overridden definitions higher up the hierarchy.
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)

Last edited by CaptainMorgan : 04-02-2008 at 12:15 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
toString method apfroggy0408 New To Java 6 01-31-2008 06:08 AM
Inheritance mew New To Java 1 12-07-2007 08:08 PM
Arrays.toString Java Tip Java Tips 0 12-01-2007 11:35 PM
Can i just use toString? cachi New To Java 1 07-31-2007 10:32 PM
Inheritance in GUI Marty SWT / JFace 2 05-11-2007 02:54 AM


All times are GMT +3. The time now is 12:02 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org