Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Method in subclass is not being implemented.

    My java code compiles but when the program runs, the statement:

    Java Code:
    System.out.println("Account Owner2 Balance with interest:"  
    
    			+ owner[1].add_monthly_interest());
    displays as "Account Owner2 balance with interest: 0"

    It's not implementing the add_monthly_interest() method in the SavingsAccount subclass. That method should be inherited from the Account class.

    Java Code:
    class Account {
    
    //Declare the variables
    
    	private String name;
    	protected double balance;
    	private double deposit;	
    	private double withdraw;
    	protected double intrate;
    
    //Declare getter and setter methods
    
    	public String getName()			{
    		return name;
    	}
    
    	public double getDeposit()		{
    		return deposit;
    	}
    
    	public double getWithraw()		{
    		return withdraw;							
    	}
    
    	public double getBalance()		{
    		return balance;
    	}
    
    	public double getIntrate()		{
    		return intrate;
    	}
    	
    	public void setName(String n)		{
    		name = n;
    	}
    
    	public void setDeposit(double d)	{
    		deposit = d;
    	}
    
    	public void setWithdraw(double w)	{
    		withdraw = w;
    	}
    
    	public void setBalance(double b)	{
    		balance = b;
    	}
    
    	public void setIntrate(double i)	{
    		intrate = i;
    	}
    
    
    //Calculate the balance after the deposit.
    
    	public double BalAfterDeposit()	{
    		double total = balance + deposit;
    		return total;
    	}
    
    //Calculate the balance after the withdraw.
    
    	public double BalAfterWithdraw() {
    
    		
    		double total = balance - withdraw;
    		return total;
    		
    	}
    
    	public double add_monthly_interest()	{
    		return 0;	
    	}
    
    
    	
    }
    
    
    	
    
    	class AccountTestDrive	{
    
    	public static void main (String[] args)	{
    
    	//Create an array that holds 2 new accounts
    
    		Account[] owner;
    		owner = new Account[2];
    
    		owner[0] = new Account();
    
    			owner[0].setName("Joe");
    			owner[0].setBalance(5000);
    			owner[0].setDeposit(7000);
    			owner[0].setWithdraw(1000);
    
    			System.out.println("Account Owner1 Name:" + 			
    
    			owner[0].getName());
    
    			System.out.println("Account Owner1 Current Balance:" + 		
    
    			owner[0].getBalance());
    
    			System.out.println("Account Owner1 Balance after Deposit:" 	
    
    			+ owner[0].BalAfterDeposit());
    
    			System.out.println("Account Owner1 Balance after Withdraw:" 
    
    			+ owner[0].BalAfterWithdraw());
    
    			
    
    
    		owner[1] = new Account();
    
    			owner[1].setName("Mary");
    			owner[1].setBalance(3000);
    			owner[1].setDeposit(4000);
    			owner[1].setWithdraw(2000);
    
    			System.out.println("Account Owner2 Name:" + 			
    
    			owner[1].getName());
    
    			System.out.println("Account Owner2 Current Balance:" + 		
    
    			owner[1].getBalance());
    
    			System.out.println("Account Owner2 Balance after Deposit:" 	
    
    			+ owner[1].BalAfterDeposit());
    
    			System.out.println("Account Owner2 Balance after Withdraw:" 
    
    			+ owner[1].BalAfterWithdraw());
    			
    			System.out.println("Account Owner2 Balance with interest:"  
    
    			+ owner[1].add_monthly_interest());
    
    			
    
    			
    			
    			
    	}
      }
    	
    	class SavingsAccount extends Account	{
    	 public double add_monthly_interest() {
    		double intrate = 0.05;
    		double total = balance + (balance * intrate)/12;
    		return total;
    	   }
    		 			        
    		
    	}

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    It's not implementing the add_monthly_interest() method in the SavingsAccount subclass.
    Where is there an instance of the SavingsAccount class? You need to define an instance if you want to use its methods.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Method in subclass is not being implemented.

    You mean this?:

    Java Code:
    class SavingsAccount extends Account    {
         public double add_monthly_interest() {
            double intrate = 0.05;
            double total = balance + (balance * intrate)/12;
            return total;
           }
                                 
             
        }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    That looks like a definition for a class. An instance of a class is created by using a new statement. Lines 91 and 117 define instance of the Account class.
    Where does the code create an instance of the SavingsAccount class?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Method in subclass is not being implemented.

    Since SavingsAccount is a subclass of Account, the methods and instances would already be inherited.

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    Do you understand what the new statement does?

    If you do not create an instance of a class, you can not call its methods. There is no instance of the SavingsAccount created so you can not call its classes.

    You can create an instance of the Account class (lines 91 and 117) and call its methods. That will have nothing to do with the methods of the SavingsAccount class.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Method in subclass is not being implemented.

    I guess I'm not understanding. I thought the main purpose of inheritance was so you didn't have to re-write the same code. If the instances and methods are already declared in Account class, SavingsAccount class would inherit those.

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    You need to create an instance of the SavingsAccount class to use its methods.
    What if you defined a CheckingAccount class that also extends the Account class.
    Now there are two classes that extend Account.

    How would you tell the compiler which of the two classes you wanted to use?
    You would use a new statement to create an instance of the class you want.

    Take a look at the tutorial:
    http://docs.oracle.com/javase/tutori...heritance.html
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Method in subclass is not being implemented.

    Take a look now. I created
    Java Code:
     owner[1] = new SavingsAccount();
    			owner[1].add_monthly_interest();
    But, still getting errors. It says class, interface, or enum expected at 146 and 148

    Java Code:
    class Account {
    
    //Declare the variables
    
    	private String name;
    	protected double balance;
    	private double deposit;	
    	private double withdraw;
    	protected double intrate;
    
    //Declare getter and setter methods
    
    	public String getName()			{
    		return name;
    	}
    
    	public double getDeposit()		{
    		return deposit;
    	}
    
    	public double getWithraw()		{
    		return withdraw;							
    	}
    
    	public double getBalance()		{
    		return balance;
    	}
    
    	public double getIntrate()		{
    		return intrate;
    	}
    	
    	public void setName(String n)		{
    		name = n;
    	}
    
    	public void setDeposit(double d)	{
    		deposit = d;
    	}
    
    	public void setWithdraw(double w)	{
    		withdraw = w;
    	}
    
    	public void setBalance(double b)	{
    		balance = b;
    	}
    
    	public void setIntrate(double i)	{
    		intrate = i;
    	}
    
    
    //Calculate the balance after the deposit.
    
    	public double BalAfterDeposit()	{
    		double total = balance + deposit;
    		return total;
    	}
    
    //Calculate the balance after the withdraw.
    
    	public double BalAfterWithdraw() {
    
    		
    		double total = balance - withdraw;
    		return total;
    		
    	}
    
    	public double add_monthly_interest()	{
    		return 0;	
    	}
    
    
    	
    }
    
    
    	
    
    	class AccountTestDrive	{
    
    	public static void main (String[] args)	{
    
    	//Create an array that holds 2 new accounts
    
    		Account[] owner;
    		owner = new Account[2];
    
    		owner[0] = new Account();
    
    			owner[0].setName("Joe");
    			owner[0].setBalance(5000);
    			owner[0].setDeposit(7000);
    			owner[0].setWithdraw(1000);
    
    			System.out.println("Account Owner1 Name:" + 			
    
    			owner[0].getName());
    
    			System.out.println("Account Owner1 Current Balance:" + 		
    
    			owner[0].getBalance());
    
    			System.out.println("Account Owner1 Balance after Deposit:" 	
    
    			+ owner[0].BalAfterDeposit());
    
    			System.out.println("Account Owner1 Balance after Withdraw:" 
    
    			+ owner[0].BalAfterWithdraw());
    
    			
    
    
    		owner[1] = new Account();
    
    			owner[1].setName("Mary");
    			owner[1].setBalance(3000);
    			owner[1].setDeposit(4000);
    			owner[1].setWithdraw(2000);
    
    			System.out.println("Account Owner2 Name:" + 			
    
    			owner[1].getName());
    
    			System.out.println("Account Owner2 Current Balance:" + 		
    
    			owner[1].getBalance());
    
    			System.out.println("Account Owner2 Balance after Deposit:" 	
    
    			+ owner[1].BalAfterDeposit());
    
    			System.out.println("Account Owner2 Balance after Withdraw:" 
    
    			+ owner[1].BalAfterWithdraw());
    
    			owner[1] = new SavingsAccount();
    			owner[1].add_monthly_interest();
    			
    			
    
    			
    
    			
    			
    			
    	}
      }
    	
    	class SavingsAccount extends Account	{
    		double intrate = 0.05;
    	 	public double add_monthly_interest() {
    		double total = balance + (balance * intrate)/12;
    		return total;
    
    		}
    	   }
    		
    	System.out.println("Account Owner2 Balance with interest:"  			
    
    	+ owner[1].add_monthly_interest()); 			        
    		
    	}

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    class, interface, or enum expected at 146 and 148
    Lines 146 and 148 are empty in the posted code.

    The { and }s are not properly aligned making the code hard to read.

    Which } is at the end of the method's definition
    and which } is at the end of the class's definition?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Method in subclass is not being implemented.

    Re-aligned the } but still getting the errors "class, enum or interface expected" at the statement

    Java Code:
    System.out.println("Account Owner2 Balance with interest:"  			
    
    + owner[1].add_monthly_interest());
    Also get the error illegal start of an expression and ] expected at

    Java Code:
    Account owner[1] = new SavingsAccount();

    Java Code:
    class Account {
    
    //Declare the variables
    
    	private String name;
    	protected double balance;
    	private double deposit;	
    	private double withdraw;
    	protected double intrate;
    
    //Declare getter and setter methods
    
    	public String getName()			{
    		return name;
    	}
    
    	public double getDeposit()		{
    		return deposit;
    	}
    
    	public double getWithraw()		{
    		return withdraw;							
    	}
    
    	public double getBalance()		{
    		return balance;
    	}
    
    	public double getIntrate()		{
    		return intrate;
    	}
    	
    	public void setName(String n)		{
    		name = n;
    	}
    
    	public void setDeposit(double d)	{
    		deposit = d;
    	}
    
    	public void setWithdraw(double w)	{
    		withdraw = w;
    	}
    
    	public void setBalance(double b)	{
    		balance = b;
    	}
    
    	public void setIntrate(double i)	{
    		intrate = i;
    	}
    
    
    //Calculate the balance after the deposit.
    
    	public double BalAfterDeposit()	{
    		double total = balance + deposit;
    		return total;
    	}
    
    //Calculate the balance after the withdraw.
    
    	public double BalAfterWithdraw() {
    
    		
    		double total = balance - withdraw;
    		return total;
    		
    	}
    
    	public double add_monthly_interest()	{
    		return 0;	
    	}
    
    
    	
    }
    
    
    	
    
    	public class AccountTestDrive	{
    
    		public static void main (String[] args)	{
    
    	//Create an array that holds 2 new accounts
    
    		Account[] owner;
    		owner = new Account[2];
    
    		owner[0] = new Account();
    
    			owner[0].setName("Joe");
    			owner[0].setBalance(5000);
    			owner[0].setDeposit(7000);
    			owner[0].setWithdraw(1000);
    
    			System.out.println("Account Owner1 Name:" + 			
    
    			owner[0].getName());
    
    			System.out.println("Account Owner1 Current Balance:" + 		
    
    			owner[0].getBalance());
    
    			System.out.println("Account Owner1 Balance after Deposit:" 	
    
    			+ owner[0].BalAfterDeposit());
    
    			System.out.println("Account Owner1 Balance after Withdraw:" 
    
    			+ owner[0].BalAfterWithdraw());
    
    			
    
    
    		owner[1] = new Account();
    
    			owner[1].setName("Mary");
    			owner[1].setBalance(3000);
    			owner[1].setDeposit(4000);
    			owner[1].setWithdraw(2000);
    
    			System.out.println("Account Owner2 Name:" + 			
    
    			owner[1].getName());
    
    			System.out.println("Account Owner2 Current Balance:" + 		
    
    			owner[1].getBalance());
    
    			System.out.println("Account Owner2 Balance after Deposit:" 	
    
    			+ owner[1].BalAfterDeposit());
    
    			System.out.println("Account Owner2 Balance after Withdraw:" 
    
    			+ owner[1].BalAfterWithdraw());
    
    		Account owner[1] = new SavingsAccount();
    		owner[1].add_monthly_interest();
    			
    			
    			
    		}
      	}
    	
    	class SavingsAccount extends Account	{
    		double intrate = 0.05;
    	 	public double add_monthly_interest() {
    		double total = balance + (balance * intrate)/12;
    		return total;
    
    		}
    	   }
    		
    	System.out.println("Account Owner2 Balance with interest:"  			
    
    	+ owner[1].add_monthly_interest()); 			        
    		
    	}

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    Is that line of code inside of a method? It looks like its outside of any method definition.
    Most executable statements must be coded inside of a method.

    When do you want that statement executed? Put it in a method where it will be executed at the time that you want it to be executed.

    Java Code:
    Account owner[1] = new SavingsAccount();
    What is that statement supposed to do? See statements on lines 91 and 117 for how to define an instance of a class and assign its reference value to an element of an array.
    Last edited by Norm; 09-27-2013 at 03:43 AM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Method in subclass is not being implemented.

    I got it to compile again, but, again "Balance with interest" displays 0. SavingsAccount is a subclass of Account, but, obviously the balance of owner[1] is not being recognized in the add_monthly_interest() method. I'm thinking it's the variable I declared in my new object, SavingsAccount owner2 = new SavingsAccount();. I can't put owner[1] in there, because then I get an illegal start of an expression error.

    Java Code:
    class Account {
    
    //Declare the variables
    
    	private String name;
    	protected double balance;
    	private double deposit;	
    	private double withdraw;
    	protected double intrate;
    
    //Declare getter and setter methods
    
    	public String getName()			{
    		return name;
    	}
    
    	public double getDeposit()		{
    		return deposit;
    	}
    
    	public double getWithraw()		{
    		return withdraw;							
    	}
    
    	public double getBalance()		{
    		return balance;
    	}
    
    	public double getIntrate()		{
    		return intrate;
    	}
    	
    	public void setName(String n)		{
    		name = n;
    	}
    
    	public void setDeposit(double d)	{
    		deposit = d;
    	}
    
    	public void setWithdraw(double w)	{
    		withdraw = w;
    	}
    
    	public void setBalance(double b)	{
    		balance = b;
    	}
    
    	public void setIntrate(double i)	{
    		intrate = i;
    	}
    
    
    //Calculate the balance after the deposit.
    
    	public double BalAfterDeposit()	{
    		double total = balance + deposit;
    		return total;
    	}
    
    //Calculate the balance after the withdraw.
    
    	public double BalAfterWithdraw() {
    
    		
    		double total = balance - withdraw;
    		return total;
    		
    	}
    
    	public double add_monthly_interest()	{
    		return 0;	
    	}
    
    
    	
    }
    
    
    	
    
    	class AccountTestDrive	{
    
    		public static void main (String[] args)	{
    
    	//Create an array that holds 2 new accounts
    
    		Account[] owner;
    		owner = new Account[2];
    
    		owner[0] = new Account();
    
    			owner[0].setName("Joe");
    			owner[0].setBalance(5000);
    			owner[0].setDeposit(7000);
    			owner[0].setWithdraw(1000);
    
    			System.out.println("Account Owner1 Name:" + 			
    
    			owner[0].getName());
    
    			System.out.println("Account Owner1 Current Balance:" + 		
    
    			owner[0].getBalance());
    
    			System.out.println("Account Owner1 Balance after Deposit:" 	
    
    			+ owner[0].BalAfterDeposit());
    
    			System.out.println("Account Owner1 Balance after Withdraw:" 
    
    			+ owner[0].BalAfterWithdraw());
    
    			
    
    
    		owner[1] = new Account();
    
    			owner[1].setName("Mary");
    			owner[1].setBalance(3000);
    			owner[1].setDeposit(4000);
    			owner[1].setWithdraw(2000);
    
    			System.out.println("Account Owner2 Name:" + 			
    
    			owner[1].getName());
    
    			System.out.println("Account Owner2 Current Balance:" + 		
    
    			owner[1].getBalance());
    
    			System.out.println("Account Owner2 Balance after Deposit:" 	
    
    			+ owner[1].BalAfterDeposit());
    
    			System.out.println("Account Owner2 Balance after Withdraw:" 
    
    			+ owner[1].BalAfterWithdraw());
    
    				SavingsAccount owner2 = new SavingsAccount();
    				double aBal = owner2.add_monthly_interest();
    				System.out.println("Balance with interest:" + 		
    
    				aBal);
    
    		
    		}
      	}
    	
    	class SavingsAccount extends Account	{
    		double intrate = 0.05;
    	 	public double add_monthly_interest() {
    		double a_Balance = balance + (balance * intrate)/12;
    		return a_Balance;
    		}
    	}

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    Since the SavingsAccount class extends the Account class, you can assign a reference to a SavingsAccount object to the owner array.

    What does the println() on line 142 print out?

    How do any of the variables in the owner2 object get values? The code creates the owner2 object and never assigns any values to its variables like the code in lines 119 to 122 does for owner[1]


    the balance of owner[1] is not being recognized
    The contents of the variables in owner[1] are not the same as the contents of the variables in the owner2 object.
    Each object has its own variables with their own values.
    Last edited by Norm; 09-28-2013 at 04:31 AM.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Method in subclass is not being implemented.

    Line 142 prints out as "Balance with interest: 0". Yes, I want to get the balance value from owner[1] and then pass it into the add_monthly_interest() method. But, if I write line 140 as SavingsAccount owner[1] = new SavingsAccount(); I get an error.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    SavingsAccount owner[1] = new SavingsAccount();
    The variable: owner is alread defined as an array of type Account on line: 88.
    All you need to do is assign a reference to a new SavingsAccount object to one of its elements:
    Java Code:
    owner[1] = new SavingsAccount();
    Now owner[1] has a reference to an instance of a SavingsAccount object in it.
    Don't try do redefine the owner variable by putting SavingsAccount in front of it.

    You need to take a look at how to define variables and how to assign values to variables.

    Take another look at the tutorial: The Really Big Index
    Last edited by Norm; 09-28-2013 at 05:00 AM.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Method in subclass is not being implemented.

    It compiles but take a look at lines 141 and 142. Before I had those statements in there, line 143 printed as "Account Owner2 Balance with interest: 0" It seems to defeat the whole purpose of having the subclass because in this case, the subclass did not inherit the values of the balance variables declared in Account().


    Java Code:
    class Account {
    
    //Declare the variables
    
    	private String name;
    	protected double balance;
    	private double deposit;	
    	private double withdraw;
    	protected double intrate;
    
    //Declare getter and setter methods
    
    	public String getName()			{
    		return name;
    	}
    
    	public double getDeposit()		{
    		return deposit;
    	}
    
    	public double getWithraw()		{
    		return withdraw;							
    	}
    
    	public double getBalance()		{
    		return balance;
    	}
    
    	public double getIntrate()		{
    		return intrate;
    	}
    	
    	public void setName(String n)		{
    		name = n;
    	}
    
    	public void setDeposit(double d)	{
    		deposit = d;
    	}
    
    	public void setWithdraw(double w)	{
    		withdraw = w;
    	}
    
    	public void setBalance(double b)	{
    		balance = b;
    	}
    
    	public void setIntrate(double i)	{
    		intrate = i;
    	}
    
    
    //Calculate the balance after the deposit.
    
    	public double BalAfterDeposit()	{
    		double total = balance + deposit;
    		return total;
    	}
    
    //Calculate the balance after the withdraw.
    
    	public double BalAfterWithdraw() {
    
    		
    		double total = balance - withdraw;
    		return total;
    		
    	}
    
    	public double add_monthly_interest()	{
    		return 0;	
    	}
    
    
    	
    }
    
    
    	
    
    	class AccountTestDrive	{
    
    		public static void main (String[] args)	{
    
    	//Create an array that holds 2 new accounts
    
    		Account[] owner;
    		owner = new Account[2];
    
    		owner[0] = new Account();
    
    			owner[0].setName("Joe");
    			owner[0].setBalance(5000);
    			owner[0].setDeposit(7000);
    			owner[0].setWithdraw(1000);
    
    			System.out.println("Account Owner1 Name:" + 			
    
    			owner[0].getName());
    
    			System.out.println("Account Owner1 Current Balance:" + 		
    
    			owner[0].getBalance());
    
    			System.out.println("Account Owner1 Balance after Deposit:" 	
    
    			+ owner[0].BalAfterDeposit());
    
    			System.out.println("Account Owner1 Balance after Withdraw:" 
    
    			+ owner[0].BalAfterWithdraw());
    
    			
    
    
    		owner[1] = new Account();
    
    			owner[1].setName("Mary");
    			owner[1].setBalance(3000);
    			owner[1].setDeposit(4000);
    			owner[1].setWithdraw(2000);
    
    			System.out.println("Account Owner2 Name:" + 			
    
    			owner[1].getName());
    
    			System.out.println("Account Owner2 Current Balance:" + 		
    
    			owner[1].getBalance());
    
    			System.out.println("Account Owner2 Balance after Deposit:" 	
    
    			+ owner[1].BalAfterDeposit());
    
    			System.out.println("Account Owner2 Balance after Withdraw:" 
    
    			+ owner[1].BalAfterWithdraw());
    
    			owner[1] = new SavingsAccount();
    			owner[1].setBalance(3000);
    			owner[1].getBalance();
    			System.out.println("Account Owner2 Balance with Interest:"
    			+ owner[1].add_monthly_interest());
    				
    
    		
    		}
      	}
    	
    	class SavingsAccount extends Account	{
    		double intrate = 0.05;
    	 	public double add_monthly_interest() {
    		double a_Balance = balance + (balance * intrate)/12;
    		return a_Balance;
    		}
    	}

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    You should post the output that the program creates and add some comments saying what is wrong with the output.

    You need to understand that each instance of a class has its own variables which will contain their own values.
    On line 117 an instance of the Account class is created and it reference is saved in owner[1].
    The variables in that instance are given values in the code on lines 119 to 122

    On line 140 an instance of the SavingsAccount class is created and it reference is saved in owner[1], replacing the reference to the Account class object created on line 117.
    On line 141 a value is given to that instance.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    MichiKen is offline Member
    Join Date
    Sep 2013
    Posts
    19
    Rep Power
    0

    Default Re: Method in subclass is not being implemented.

    Quote Originally Posted by Norm View Post
    You should post the output that the program creates and add some comments saying what is wrong with the output.
    Here's the output:

    Account Owner1 Name: Joe
    Account Owner1 Current Balance:5000.0
    Account Owner1 Balance after Deposit:12000.0
    Account Owner1 Balance after Withdraw: 4000.0
    Account Owner2 Name: Mary
    Account Owner2 Current Balance:3000.0
    Account Owner2 Balance after Deposit: 7000.0
    Account Owner2 Balance after Withdraw: 1000.0
    Account Owner2 Balance with Interest: 3012.5

    The value of balance that is plugged into the math formula in the add_monthly_interest() method is on line 141. But, since class SavingsAccount is a subclass of Account, I thought SavingsAccount would INHERIT the value of the balance variable on line 121, thus, eliminating the need to write the lines of code on lines 141 and 142. Is my concept of inheritance wrong?

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,585
    Rep Power
    25

    Default Re: Method in subclass is not being implemented.

    How does the data that is in the instance of Account created on line 117 get into the instance of SavingsAccount created on line 140? What about the values saved in the instance of Account created on line 91?
    The code has 3 different instances of a class and each has its own values.

    What if the code creates more than one instance of the SavingsAccount class?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2013, 04:09 AM
  2. Subclass and Abstract method implementation
    By konman795 in forum New To Java
    Replies: 2
    Last Post: 02-24-2012, 09:44 AM
  3. Replies: 23
    Last Post: 01-17-2012, 07:16 PM
  4. Can not find subclass method
    By samanesh in forum New To Java
    Replies: 4
    Last Post: 11-24-2011, 05:26 PM
  5. Can an Interface have implemented method???
    By pppriyadarshi in forum New To Java
    Replies: 1
    Last Post: 09-10-2011, 06:44 PM

Posting Permissions

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