Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    62
    Rep Power
    0

    Default 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:

    Java Code:
    public void credit(double money);
    {
    super.credit();
    //and then expand on the method here
    The credit method in A is written the same public void credit(double money);

    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:

    Java Code:
    public void credit(double money);
    {
    super.credit(double money)
    //and then expand on the method here
    I then get the error message: .class expected.

    All I can say is AAAAAAAH!

    Any help would be gratefully received.

    Cheers - FMJ.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java 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
    or, you can just do
    Java Code:
    public void credit(double money) {
        // do what you like...

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

    Default

    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.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    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

  5. #5
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    7

    Default

    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

  1. [SOLVED] Method Overriding
    By MithunDhar in forum New To Java
    Replies: 3
    Last Post: 04-06-2009, 09:02 AM
  2. Replies: 1
    Last Post: 04-01-2009, 03:00 AM
  3. Replies: 1
    Last Post: 02-10-2009, 11:03 AM
  4. is overriding static method possible
    By raghu in forum Advanced Java
    Replies: 1
    Last Post: 01-22-2008, 01:38 AM
  5. I am confused about is the "add" method...
    By mathias in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 06:29 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
  •