Results 1 to 5 of 5
- 05-23-2009, 02:01 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 62
- Rep Power
- 0
Method Overriding - Seriously confused :-(
Dear All,
I have two account classes say A and B. B extends A. I am trying to re-write a method in B that is also in A but I would like to expand on this method greatly. The problem is that my coursebooks tell me to do this:
The credit method in A is written the same public void credit(double money);Java Code:public void credit(double money); { super.credit(); //and then expand on the method here
When I try and compile the code - I get the error:
credit(double) in A cannot be applied to (). I then try and change the code to:
I then get the error message: .class expected.Java Code:public void credit(double money); { super.credit(double money) //and then expand on the method here
All I can say is AAAAAAAH!
Any help would be gratefully received.
Cheers - FMJ.
- 05-23-2009, 02:12 AM #2
or, you can just doJava Code:public void credit(double money) { ... } public void credit(double money) { super.credit(25.00); // any double value argument is okay //and then expand on the method here
Java Code:public void credit(double money) { // do what you like...
- 05-23-2009, 02:15 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
Watch the semicolons. They should appear at the end of statements. They should not appear in the middle of method definitions.
Compiler messages can be confusing to begin with. A SSCCE will help clarify things if you post the exact and entire compiler message you can't understand along with an indication of which lines in your code it is referring to.
- 05-23-2009, 03:10 AM #4
How many times - go through the tutorials.
Calling the super method is exactly the same as calling any method. You don't put a type in the arguments for a method call.Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 05-26-2009, 04:18 PM #5
here is a little code example. pay attention to the comments in the code.
As you have it now calling super.credit(money) wont work unless you have something similar to my method at (1)
Java Code:public class B { public void credit(){ //do stuff in here } //(1) public void credit(double money){ //overloaded methods(?) is where you have multiple methods with the same name and different parameters } } class A extends B{ public void credit(double money){ //this is accepted super.credit(); //this isn't. In order to override the method //you have to have a method with those same parameters in your parent method.(1) //This method however won't be of too much use unless B.credit() is changed from //a void type to a double type. super.credit(money); } }Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
Similar Threads
-
[SOLVED] Method Overriding
By MithunDhar in forum New To JavaReplies: 3Last Post: 04-06-2009, 08:02 AM -
Overriding a method of a JPanel which was visually added to a container
By Pucho in forum NetBeansReplies: 1Last Post: 04-01-2009, 02:00 AM -
static method sparks error on overriding non-static method
By MuslimCoder in forum New To JavaReplies: 1Last Post: 02-10-2009, 10:03 AM -
is overriding static method possible
By raghu in forum Advanced JavaReplies: 1Last Post: 01-22-2008, 12:38 AM -
I am confused about is the "add" method...
By mathias in forum New To JavaReplies: 1Last Post: 08-01-2007, 05:29 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks