3 Replies - 478 Views - Last Post: 09 November 2009 - 10:40 PM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

overriding methods

Posted 09 November 2009 - 10:18 PM

The withdraw() method in SavingsAccount is supposed to override the one in Account but it doesn't seem to. I put the two System.out.println()'s in each withdraw() method to see exactly which one was being called and it is the one in Account.

isn't if there is a method in a subclass with the exact same signature, the subclass one is suppose to be called over the super?

Account
public class Account
{
//Declaring instance variables.
	private int accountNumber;
	private String ownersName;
	private double interestRate;
	protected double balance;
	
	public Account(int aNum, String oName, double iRate, double bal)
	{
//User initializing the instance variables.
		accountNumber = aNum;
		ownersName = oName;
		interestRate = iRate;
		if(bal < 0.0)
		{
			balance = 0.0;
		}
		else
		{
			balance = bal;
		}
	}

	public void withdraw(double amt)
	{
		System.out.println("HUH?");
		if(amt > 0.0)
		{
			balance = balance - amt;
		}
		else
		{
			balance = balance;
		}
	}



SavingsAccount
public class SavingsAccount extends Account
{
//Initiazling variables.
	private int countWithdrawals;

	public SavingsAccount(int aNum, String oName, double iRate, double bal)
	{
//Declaring variables and calling superclass constructor.
		super(aNum, oName, iRate, bal);
		countWithdrawals = 0;
	}		
	
//Withdraws specified amount and penalties, if any, and increments the cout total.
	public void withdraw(int number, double amt)
	{
		countWithdrawals++;
		System.out.println("boo");
		balance = balance - amt;
		if(countWithdrawals > 3)
		{
			balance = balance - 30.0;
		}
		if(balance < 0.0)
		{
			balance = balance - 35.0;
		}
	}
	
//The addition to the super's toString() in Account.
	public String toString()
	{
		return "Savings Account\n" + super.toString() + "\nWithdrawals Made: " + countWithdrawals;
	}	
}



Is This A Good Question/Topic? 0
  • +

Replies To: overriding methods

#2 TriggaMike  Icon User is offline

  • Using up all your 1's and 0's
  • member icon

Reputation: 85
  • View blog
  • Posts: 1,103
  • Joined: 26-September 08

Re: overriding methods

Posted 09 November 2009 - 10:22 PM

In savings account the withdraw method has a different method signature. The term "overriding" a method means that you have a child class that shares the EXACT same method signature. You would need to add a method that accepts only an amount like in Account.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: overriding methods

Posted 09 November 2009 - 10:23 PM

The withdraw method is your SavingAccount class has that signature
public void withdraw(int number, double amt)
your withdraw method in your Account class has that signature
public void withdraw(double amt)

they won't overload each ones they do not have the same signature (arguments parameters)

This post has been edited by pbl: 09 November 2009 - 10:28 PM

Was This Post Helpful? 1
  • +
  • -

#4 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: overriding methods

Posted 09 November 2009 - 10:40 PM

duh, knew that. didn't realize signatures were different. thanks that was the last part of my project i didn't understand. submitted it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1