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

Write a good explanation and 2 or more differences that you can see in attachmen

ID: 3802846 • Letter: W

Question

Write a good explanation and 2 or more differences that you can see in attachment. Thank you!
This is Figure 9.11 Class BasePluscommissionEnployee Class BasePluscomnissionEnployee (Fig. 9.9) extends the new version of class commis- sionEnployee with protected instance variables. BasePluscommissionEnployee objects inherit CommissionEmployee's protected instance variables firstNane, lastName, so cialSecurityNumber, grossSales and commissionRate-all these variables are now pro- tected members of BasePlusconmissionEmployee. As a result, the compiler does not generate errors when compiling line 45 of method earnings and lines 54-56 of method tostring. If another class extends this version of class BasePluscommissionEmployee, the new subclass also can access the protected members. I Fig. 9.9: BasePluscommissionEmployee.java 2 Base Plus CommissionEmployee inherits protected instance 3 variables from CommissionEmployee. 5 public class BasePlusCommissionEmployee extends CommissionEmployee 7 private double basesalary base salary per week Fig. 9.9 l BasePluscommissionEmployee inherits protected instance variables from Commission Employee. (Part I of 2) 380 Chapter 9 Object-Oriented Programming: inheritance 11 six-a rgument constructor 10 public Base PlusC firstName, String lastName ommissionEmployee(String String social Securit r, double gros sSales double commissionRate, double baseSalary) super(firstName, lastNane, socialSecurityNumber. 14 gross Sales commission Rate) 15 16 if basesalary is invalid throw exception if (baseSalary 0.0) throw new niegalArgumentExceptionC "Base salary must be 0.0"); this basesalary base salary 25 set base salary public void setBasesalaryCdouble basesalary) if (base salary 0.0) throw new niega ArgumentException( "Base salary must be 0-0"); this basesalary base salary return base salary public double getBasesalaryO base salary return

Explanation / Answer

To understand the differences between above Figure 9.9 and Figure 9.11 we need to understand about private and protected members in java.

Protected -
If a Variables, methods, and constructors, which are declared protected in a superclass then there can be 2 cases:
If subclass is in same package:
Can access through inheritance
Can access by creating an instance of parent class
If subclass is in different package:
Can only access through inheritance

Private-

If a Methods, variables, and constructors that are declared private can only be accessed within the declared class itself. We can access private methods outside the class using public getter methods .

In fig:9.9 BasePlusCommissionEmployee extended CommissionEmployee class in which firstname,lastname,socialSecurityNumber,grossSales,commissionRate are protected members of base class
For eg: In this example we are calculating earnings by directly accessing base variables
public double earnings(){
   return basesalary +(commissionRate * grossSales);
}
Similarly ,in toString method of subclass we can use protected members directly .

In fig:9.11 BasePlusCommissionEmployee extended CommissionEmployee class in which firstname,lastname,socialSecurityNumber,grossSales,commissionRate are private members of base class. So,these can be accessed by public methods of base class.
For eg: In this example we are calculating earnings by calling public method of base class using super keyword(super keyword used for calling base class members in subclass.Only public and protected methods can be called by the super keyword.)
public double earnings(){
   return getBasesalary() + super.earnings();
}
Similarly ,in toString method of subclass we cannot use private members directly thats why we called super.toString()

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote