Results 1 to 8 of 8
  1. #1
    zach&kody is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Exclamation Override a superclass's methods with a subclass

    Hey everyone, I am part of a first year computer science class, and my friend and I are working on a project where we have to override a superclass's methods with a subclass. We have tried numerous techniques, but we have run out of ideas. Thanks in advance for all of your help. -From our computer to yours- zach&kody

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Here is a quick example.
    Java Code:
    class X{
      public void someMethod(){
        print 50 - 75
      }
    }
    
    class Y extends X{
      public void someMethod(){
        Print 25 - 50
      }
    }
    The method has the same name, but the subclass does something different. Read up on the tutorials on inheritance for more information.

  3. #3
    goldest's Avatar
    goldest is offline Senior Member
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    469
    Rep Power
    5

    Wink

    Zach & Kody,

    Go through this article which will give you more details about Overriding, Overloading, along with their rules and differences.

    Here is the link : Method Overriding - Overloading

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!
    Click on * and add to member reputation, if you find their advices/solutions effective.

  4. #4
    zach&kody is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Exclamation This is what I have so far

    This is the superclass that I am trying to override.
    Java Code:
    // This class represents a walker with two feet.
    
    import java.awt.Image;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    public class Walker extends JPanel
    {
      public static final int PIXELS_PER_INCH = 6;
      public Foot leftFoot, rightFoot;
      public int stepLength;
      public int stepsCount;
    
      // Constructor
      public Walker(int x, int y, Image leftPic, Image rightPic)
      {
        leftFoot =  new Foot(x, y - PIXELS_PER_INCH * 4, leftPic);
        rightFoot = new Foot(x, y + PIXELS_PER_INCH * 4, rightPic);
        stepLength = PIXELS_PER_INCH * 12;
      }
    
      // Returns the left foot
      public Foot getLeftFoot()
      {
        return leftFoot;
      }
    
      // Returns the right foot
      public Foot getRightFoot()
      {
        return rightFoot;
      }
    
      // Makes first step, starting with the left foot
      public void firstStep()
      {
        leftFoot.moveForward(stepLength);
        stepsCount = 1;
      }
    
      // Makes next step
      public void nextStep()
      {
        if (stepsCount % 2 == 0)  // if stepsCount is even
          leftFoot.moveForward(2 * stepLength);
        else
          rightFoot.moveForward(2 * stepLength);
    
        stepsCount++;  // increment by 1
      }
    
      // Stops this walker (brings its feet together)
      public void stop()
      {
        if (stepsCount % 2 == 0) // if stepsCount is even
          leftFoot.moveForward(stepLength);
        else
          rightFoot.moveForward(stepLength);
    
        stepsCount++;  // increment by 1
      }
    
      // Returns the distance walked
      public int distanceTraveled()
      {
        return stepsCount * stepLength;
      }
    
      // Draws this walker
      public void draw(Graphics g)
      {
        rightFoot.draw(g);
        leftFoot.draw(g);
      }
    }
    This is the subcass of which I am trying to override the superclass
    Java Code:
    // This class represents a subclass of Walker.
    
    import java.awt.Image;
    import java.awt.Graphics;
    
    public class Bystander extends Walker
    {
      public static final int PIXELS_PER_INCH = 6;
      private Foot lf, rf;
      private int stepLength;
      public int tapsCount;
    
      // Constructor
      public Bystander (int x, int y, Image leftPic, Image rightPic)
      {
        super(x, y, leftPic, rightPic);
        Foot lf = getLeftFoot();
        Foot rf = getRightFoot();
      }
    
      // Returns the left foot
      public Foot getLeftFoot()
      {
        return lf;
      }
    
      // Returns the right foot
      public Foot getRightFoot()
      {
        return rf;
      }
    
      // Makes first tap, starting with the left foot
      public void firstStep()
      {
        lf.turn(-45);
        tapsCount = 1;
      }
    
      // Makes next tap
      public void nextStep()
      {
        if (tapsCount % 2 == 0)  // if stepsCount is even
          lf.turn(45);
        else
          lf.turn(-45);
    
        tapsCount++;  // increment by 1
      }
    
      // Stops this walker (brings its feet together)
      public void stop()
      {
        if (tapsCount % 2 == 0) // if tapsCount is even
          lf.turn(45);
        else
          rf.turn(0);
    
        tapsCount++;  // increment by 1
      }
    
      // Returns the distance walked
      public int distanceTraveled()
      {
        return 0;
      }
    
      // Draws this walker
      public void draw(Graphics g)
      {
       super.draw(g);
      }
    }

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    When you inherit from a class by extending it, the subclass automatically gets all the superclass's variables and methods (with certain restrictions), so you don't need to duplicate all the same method code and variables in the subclass.

    Only if you want the subclass to do something different to the superclass for some particular method, do you override that method by declaring it in the subclass and providing your own subclass implementation of it.

    So the subclass should be empty apart from the methods where you want it to do something different to the superclass, and any extra variables it wants to add.

    It would help in your example, if you explained what you want to achieve by overriding superclass methods.
    Last edited by dlorde; 04-28-2011 at 05:14 PM.

  6. #6
    zach&kody is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Exclamation

    The source Walker is supposed to walk the feet across the screen, and my objective is to override Walker with Bystander to make the feet stand still and tap back and forth.

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    6

    Default

    You don't override classes, you override methods - did you read the link goldest provided?

    my objective is to ... make the feet stand still and tap back and forth
    Have you considered changing the step methods to move forward and back instead of only moving forward? IOW overriding the step methods?

  8. #8
    zach&kody is offline Member
    Join Date
    Apr 2011
    Posts
    4
    Rep Power
    0

    Default

    Thanks everyone! we have discovered the problem and we got the program to work, thanks for all of your help! -From our computer to yours'- zach&kody

Similar Threads

  1. Replies: 0
    Last Post: 03-31-2010, 05:18 AM
  2. why we cann't override static methods
    By haoberoi in forum New To Java
    Replies: 2
    Last Post: 11-11-2008, 11:07 AM
  3. How To Override Methods, NetBeans 6.1
    By markw8500 in forum NetBeans
    Replies: 1
    Last Post: 08-16-2008, 11:58 PM
  4. Override Methods In NetBeans IDE
    By JavaForums in forum NetBeans
    Replies: 0
    Last Post: 07-30-2007, 11:13 PM

Tags for this Thread

Posting Permissions

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