Results 1 to 11 of 11
  1. #1
    trying is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Override an instance method inherited from a class - Advise needed

    Hi all

    Just wanted to see if any of you good people can point me in the right direction for any tutorials or advise on overriding an instance method inherited from a class

    I have been over and over tying to complete a class called setColour and setPosition which override the original methods but just cant get it to stick, I have included details below for any advise:


    In ZombieFrog override the setColour() instance method inherited from Frog
    so that the colour of the receiver can only be set to white. If any other colour is
    given as the argument, the method should do nothing. (You should use
    OUColour.WHITE to represent the colour white.)

    Java Code:
       /**
        * Constructor for objects of the abstract class Amphibian
        */
       public Amphibian()
       {
          super();
       }
    
    /* instance methods */    
        
       /** 
        * Moves the receiver to the left 
        */ 
       public abstract void left(); 
       
       /** 
        * Moves the receiver to the right
        */
       public abstract void right();
       
       /**
       * Resets the receiver to its "home" position
       */
       public abstract void home();
       
       /**
        * Returns the position of the receiver.
        */
       public int getPosition()
       {
          return position;
       }
       
       /**
        * Sets the position of the receiver to the value of the argument aPosition
        */
       public void setPosition (int aPosition)
       {
          position = aPosition;
          this.update("position");
       }
       
       /**
        * Sets the colour of the receiver to the argument's colour.
        */
       public void sameColourAs(Amphibian anAmphibian)
       {
          this.setColour(anAmphibian.getColour());
       }
       
       /**
        * Returns the colour of the receiver.
        */
       public OUColour getColour()
       {
          return colour;
       }
       
       /**
        * Sets the colour of the receiver to the value of the argument aColour.
        */
       public void setColour(OUColour aColour)
       {
          colour = aColour;
          this.update("colour");
       }
       
       /**
        * Sets the colour of the receiver to brown.
        */
       public void brown()
       {
          this.setColour(OUColour.BROWN);
       }
       
       /**
        * Sets the colour of the receiver to green.
        */
       public void green()
       {
          this.setColour(OUColour.GREEN);
       }
       
       /**
        * Causes user interface to emit a sound.
        */
       public void croak()
       {
          this.performAction("croak");
       }
       
       /**
        * Returns a string representation of the receiver.
        */
       public String toString()
       {
          return "An instance of class "+ this.getClass().getName() 
                 + ": position " + this.getPosition() 
                 + ", colour " + this.getColour();
       }
    }
    I want to understand how to implement the method so any tutorials you might know of or advise that would be great

    James
    Last edited by pbrockway2; 01-05-2012 at 01:32 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Override an instance method inherited from a class - Advise needed

    Hi James, welcome to Java-Forums!

    If you post code it is a good idea to use the "code" tags: put [code] at the start of the code and [/code] at the end. That way the code will be properly formatted when it appears on the web page. (*)

    (* Edit) Or at least it ought to! I see there is a bug with how the forum parses comments... ;(

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Override an instance method inherited from a class - Advise needed

    In ZombieFrog override the setColour() instance method inherited from Frog
    so that the colour of the receiver can only be set to white. If any other colour is
    given as the argument, the method should do nothing.
    You haven't actually posted the code for Frog (or is that what Amphibian is supposed to be?).

    What you are doing is creating a new class ZombieFrog. Use the keyword "extends" so that it is a subclass of Frog (or Amphibian). The just write the setColor() to do what the instructions say. There is nothing special to overriding: you just write the setColor() method for this new class making sure it has the same arguments and returns the same sort of thing - probably void in this case - as setColor() in the parent class.

    Post your code for both classes if you get stuck.

    -----

    Offhand I don't know of any good tutorials for this stuff. But others are sure to.

  4. #4
    trying is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Override an instance method inherited from a class - Advise needed

    if i am honest, i think I might be ok with overriding what I think my problem is is removing some of the methods that come with it ie a zombie frog can only be green or white where the frog which it extends can be green, brown, red etc. Im pretty sure I can do this with a if else statement:

    Java Code:
        public void setColour(OUColour aColour)
        {
           if (aColour == OUColour.WHITE)
           {
              this.update("colour");
             }
             else
             {
             NOT TOO SURE WHAT TO PUT HERE :(
             }
             super.setColour(aColour);
       }
    }
    Ahwell Ill just keep at it :)
    Last edited by trying; 01-08-2012 at 01:15 AM.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Override an instance method inherited from a class - Advise needed

    You don't need to put anything in the else block, and in fact probably don't even need it.

    You should though consider moving your super.setColour(aColour) to a better location in your method....

    Think, when do you want this super method called (a call which will actually do the work of changing the colour of the frog)?

  6. #6
    trying is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Override an instance method inherited from a class - Advise needed

    Quote Originally Posted by Fubarable View Post
    You don't need to put anything in the else block, and in fact probably don't even need it.

    You should though consider moving your super.setColour(aColour) to a better location in your method....

    Think, when do you want this super method called (a call which will actually do the work of changing the colour of the frog)?
    see I thought i might not need an else statement and tried it before posting however I am still able to change a ZombieFrog to red so maybe its something to do with the way I am implementing the if statement

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Override an instance method inherited from a class - Advise needed

    Quote Originally Posted by trying View Post
    see I thought i might not need an else statement and tried it before posting however I am still able to change a ZombieFrog to red so maybe its something to do with the way I am implementing the if statement
    ... or maybe it has more to do with my other statement in my post above? hmmm...

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Override an instance method inherited from a class - Advise needed

    In fact, I would get rid of this.update("colour"); from your override. Just call the super method, but put it in the right location.

  9. #9
    trying is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Override an instance method inherited from a class - Advise needed

    Quote Originally Posted by Fubarable View Post
    ... or maybe it has more to do with my other statement in my post above? hmmm...
    Aw..... How does:

    Java Code:
        public void setColour(OUColour aColour)
        {
           if (aColour == OUColour.WHITE)
           {
              super.setColour(aColour);
              this.update("colour");
             }
             if (aColour == OUColour.GREEN)
             {
                super.setColour(aColour);
                this.update("colour");
             }
            }
    sound?

    I had previously added the super.setColour to if (aColour == OUColour.WHITE) however I removed it as my Zombiefrog Colour was returned as null, then it clicked that this is because frogs start off green, by replicating the code .WHITE for .GREEN my frog started green and I could only change between white and green as required
    Last edited by trying; 01-08-2012 at 02:02 AM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Override an instance method inherited from a class - Advise needed

    1) Since you're calling super.setColour(aColour), do you need to call this.update("colour")? Doesn't the super's method already do this for you?
    2) So you're allowing the frog to be set to a green colour. Is this in agreement with the requirements?

  11. #11
    trying is offline Member
    Join Date
    Jan 2012
    Posts
    16
    Rep Power
    0

    Default Re: Override an instance method inherited from a class - Advise needed

    Quote Originally Posted by Fubarable View Post
    1) Since you're calling super.setColour(aColour), do you need to call this.update("colour")? Doesn't the super's method already do this for you?
    2) So you're allowing the frog to be set to a green colour. Is this in agreement with the requirements?
    this.update isnt required.

    The frog its still start green if it is dormant, and goes white if it is active ie if it is a zombiefrog

    I appreciate your help via hints, it have gave me a better understanding of how super.colour super.position etc works

Similar Threads

  1. Inherited actionPerformed method
    By doobybug in forum New To Java
    Replies: 4
    Last Post: 08-13-2011, 12:33 PM
  2. Override class method
    By Mekie in forum New To Java
    Replies: 8
    Last Post: 11-01-2010, 07:26 AM
  3. Swing application advise needed
    By cyberdesi in forum AWT / Swing
    Replies: 2
    Last Post: 08-20-2010, 01:45 AM
  4. Inherited method returning bad value
    By viking90 in forum New To Java
    Replies: 11
    Last Post: 04-07-2010, 04:53 PM
  5. Replies: 20
    Last Post: 06-03-2009, 06:33 PM

Posting Permissions

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