Results 1 to 2 of 2
  1. #1
    Kreuz14 is offline Member
    Join Date
    Jan 2008
    Posts
    5
    Rep Power
    0

    Default Inheritance, methods, and toString...

    Java 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?

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    Default

    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.
    Last edited by CaptainMorgan; 04-01-2008 at 11:15 PM.
    Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
    Want to voice your opinion on your IDE/Editor of choice? Vote now!
    Got a little Capt'n in you? (drink responsibly)

Similar Threads

  1. toString method
    By apfroggy0408 in forum New To Java
    Replies: 6
    Last Post: 01-31-2008, 05:08 AM
  2. Inheritance
    By mew in forum New To Java
    Replies: 1
    Last Post: 12-07-2007, 07:08 PM
  3. Arrays.toString
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-01-2007, 10:35 PM
  4. Can i just use toString?
    By cachi in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:32 PM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 01:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •