1 Replies - 616 Views - Last Post: 27 April 2013 - 01:21 AM Rate Topic: -----

#1 sloppy116  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-April 13

Inheritance between Base Class and Subclasses?

Posted 26 April 2013 - 09:34 PM

Hey guys, I'm working on a bank account application that consists of 3 classes. Bank Account is the base abstract class and the Checking and Savings Account are subclasses. The bank account consists of the methods of deposit and withdraw. The checking account subclass consists of "Processing a Check: debit the account by the amount on the check and decrease the number of checks by 1. The Savings account consists of determining an interest rate and adding the interest amount to the balance. The problem I'm having is trying to use the override method to "Process a Check" and "determine Interest". I used the "accountBalance" in the Bank Account class but I'm not allowed to use it in the other two accounts because it is private. What should I do to substitute accountBalance for the other two subclasses to make those specific methods work? I tried to use "aBalance" but it didn't work either in those situations I highlighted below.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


    abstract class Bank_Account           

    {
        //attributes
        private string accountNumber;
        private double [color="#4169E1"]accountBalance[/color];
        private DateTime dateAcctOpened;

        private Customer_Account myCustomer;//reference variable for Customer
    
        //constructor
        public Bank_Account(string anAccountNumber, double aBalance, DateTime aDate, Customer_Account aCustomer)
        {
            SetAccountNumber(anAccountNumber);
            SetAccountBalance(aBalance);
            SetDate(aDate);
            AssignAccountToCustomer(aCustomer); // Invoke method to assign this Account to a specific customer
        }

        public void AssignAccountToCustomer(Customer_Account aCustomer)
        {
            myCustomer = aCustomer;
           
        }

        public void SetAccountNumber(string anAccountNumber)
        {
            accountNumber = anAccountNumber;
        }

        public void SetAccountBalance(double aBalance)
        {
            accountBalance = aBalance;
        }

        public void SetDate(DateTime aDate)
        {
            dateAcctOpened = aDate;
        }

        public string GetAccountNumber()
        {
            return accountNumber;
        }

        public double GetBalance()
        {
            return accountBalance;
        }

        public DateTime GetDate()
        {
            return dateAcctOpened;
        }


        public void deposit(double amount)
        {
            double newBalance;
            newBalance = [color="#0000FF"]accountBalance[/color] + amount;
            accountBalance = newBalance;
        }


            public double WithdrawMoney(double amount)
        {
      
         double moneyAfterWithdrawal = [color="#4169E1"]accountBalance[/color]-amount;

        if(moneyAfterWithdrawal >= 0)
        {
           accountBalance = moneyAfterWithdrawal;
           return accountBalance;
        }

        throw new Exception(String.Format("Withdrawing {0} would leave you overdrawn!", amount.ToString()));
            

    }



    }
}















using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


    class Checking_Account:Bank_Account
    {
        private int numberOfChecks = 200;
      

        public Checking_Account(string anAccountNumber, double aBalance, DateTime aDate, int aCheck, Customer_Account aCustomer)
            :base(anAccountNumber, aBalance, aDate, aCustomer)
        {
            
            SetNumberOfChecks(aCheck);
        }

        private void SetNumberOfChecks(int aCheck)
        {
            numberOfChecks = aCheck;
        }

        public int GetNumberOfChecks()
        {
            return numberOfChecks;
        }


        public override double deposit(double amount)[color="#4169E1"]//ERROR[/color] "accountBalance is protected"
        {
           double newBalance;
            newBalance = accountBalance + amount;
            accountBalance = newBalance;

            numberOfChecks = -1;
        }



    }
}







using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


    class Savings_Account : Bank_Account
    {
        private double interestEarnings;

        public Savings_Account(string anAccountNumber, double aBalance, DateTime aDate, double anInterest, Customer_Account aCustomer)
            : base(anAccountNumber, aBalance, aDate, aCustomer)
        {

            SetInterestEarnings(anInterest);
        }

        public void SetInterestEarnings(double anInterest)
        {
            interestEarnings = anInterest;
        }

        public double GetInterestEarnings()
        {
            return interestEarnings;
        }

        public void interest(double interest)
        {
            if (accountBalance > 1 & SetAccountBalance < 1000) [color="#4169E1"]//ERROR[/color] "accountBalance is Protected"
                interest = .05;
            
        
    }
    
    }


Is This A Good Question/Topic? 0
  • +

Replies To: Inheritance between Base Class and Subclasses?

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,676
  • Joined: 02-June 10

Re: Inheritance between Base Class and Subclasses?

Posted 27 April 2013 - 01:21 AM

An inherited class shouldn't be using a different method. All accounts should be using the "AccountBalance" method or property from the parent method. There is a tutorial on classes linked in my signature block. Maybe it will help you clear up your misconception about what would be a new method and what wouldn't.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1