Should i use a protected variable?

So i'm working on a small program where I have to make a banking system using inheritance.

I have a bank account class, which is just, well, a general bank account. You can deposit, withdraw, and see the balance. I then have a business account class that inherits from the bank account class. The business account class is exactly the same, except that it allows overdrafts, meaning that my withdrawal function is different.

Here is the code for the two classes:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import hsa.Console;
import java.awt.*;

public class BankAccount
{
     private int balance;
     private String holder;
     private String name;
     
     public BankAccount(int balance, String holder, String name)
     {
          this.balance = balance;
          this.holder = holder;
          this.name = name;
     }
     
     public void deposite(int amount)
     {
          this.balance = this.balance + amount;
     }
     
     public void withdraw(int amount)
     {
          if(this.balance > amount)
          {
               this.balance = this.balance - amount;
          }
     }
     
     public int getBalance()
     {
          return this.balance;
     }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import hsa.Console;
import java.awt.*;

public class BusinessAcount extends BankAccount
{
     private int balance;
     private String name;
     private String holder;
     
     public BusinessAcount(int balance, String holder, String name, int x)
     {
          super(balance, name, holder);
          this.balance = balance;
     }
     
     public withdraw(int amount)
     {
          if((this.balance - amount) >= 500)
          {
               this.balance = this.balance - amount;
          }
     }
     
     public int getBalance()
     {
          return this.balance + 1;
     } 
}



However, I end up changing the balance variable for the two classes. For example, If i deposit 500 dollars, the balance variable in the BankAccount class will be different than the balance variable in the BusinessAccount class. I thought of switching balance to protected, but I don't know if this will be considered good practice. What do you guys think?

Last edited on
Do you realize this is a C++ forum?

But anyway, in your derived class, why are you duplicating member variable from the base class? You don't want lines 6-8.

Yes, make balance protected in the base class and get rid of line 13 in the derived class.

And what's going on with the redifinition of getBalance?

And check you logic in line 18. If you want to allow overdrafts, the amount can be more than the balance, not less (you probably want "-500").
closed account (48T7M4Gy)
Do you realize this is a C++ forum?


Too true. C++ is not the best place to learn about Java and inheritance (or Java and operator overloading for that matter). Each has its own rationale for doing the 'perfect' job of a programming language but on those aspects alone there is a different attitude taken.
Topic archived. No new replies allowed.