Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C) If a programmer wanted to inherit from this class and make a more specialized

ID: 3903394 • Letter: C

Question

C) If a programmer wanted to inherit from this class and make a more specialized version, what problems might the programmer face and how could they be corrected?

public class MyClass

{

   private String firstName;

   private String lastName;

   private int idNumber;

   private double balance;

public MyClass()

   {

     this.firstName = "";

     this.lastName = "";

     this.idNumber = -1;

     this.balance = 0.0;

   }

public MyClass(String firstName, String lastName, int id)

   {

     this.firstName = firstName;

     this.lastName = lastName;

     this.idNumber = id;

     this.balance = 0.0;

   }

public void makeDeposit(int id, double amount)

   {

     if(this.confirm(id))

     {

        this.balance += amount;

     }

   }

public void withdraw(int id, double amount)

   {

     if(this.confirm(id))

     {

        if(amount > this.balance)

           System.out.println("Not enough in account");

        else

           this.balance -= amount;

     }

   }

private boolean confirm(int id)

   {

     if(this.idNumber == id)

        return true;

     else

     {       

        System.out.println("INVALID");

        return false;

     }

   }

}

Explanation / Answer

If this class needs to be inherited, the programmer can run into issues with accessing the member variables namely first and last name, account id and balance.

To address these problems, these varibles need be declared as "protected" rather as "private"

protected String firstName;

protected String lastName;

protected int idNumber;

protected double balance;