Results 1 to 8 of 8
- 04-25-2011, 01:37 PM #1
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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
- 04-25-2011, 01:41 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Here is a quick example.
The method has the same name, but the subclass does something different. Read up on the tutorials on inheritance for more information.Java Code:class X{ public void someMethod(){ print 50 - 75 } } class Y extends X{ public void someMethod(){ Print 25 - 50 } }
- 04-25-2011, 02:15 PM #3
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,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 04-28-2011, 01:34 PM #4
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
This is what I have so far
This is the superclass that I am trying to override.
This is the subcass of which I am trying to override the superclassJava 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); } }
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); } }
- 04-28-2011, 05:11 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
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.
- 04-29-2011, 12:51 PM #6
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
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.
- 04-29-2011, 01:46 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
You don't override classes, you override methods - did you read the link goldest provided?
Have you considered changing the step methods to move forward and back instead of only moving forward? IOW overriding the step methods?my objective is to ... make the feet stand still and tap back and forth
- 05-24-2011, 02:50 PM #8
Member
- Join Date
- Apr 2011
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
OOP/RTII: How can I properly override AND overload methods for use with collections?
By CyJackX in forum New To JavaReplies: 0Last Post: 03-31-2010, 05:18 AM -
why we cann't override static methods
By haoberoi in forum New To JavaReplies: 2Last Post: 11-11-2008, 11:07 AM -
How To Override Methods, NetBeans 6.1
By markw8500 in forum NetBeansReplies: 1Last Post: 08-16-2008, 11:58 PM -
Override Methods In NetBeans IDE
By JavaForums in forum NetBeansReplies: 0Last Post: 07-30-2007, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks