Code reuse in Java | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Code reuse in Java

If I define class Employee with instance variables string first name,last name(general attribut of a peson or employee) and then I define new class Commissionemployee extended from employee, with class instance variables double grossSales and double commissionRate. And finally I define new class BasePlusCommissionEmployee with private instance variable double baseSalary which is extended from class CommissionEmployee, please tell me how the constructor of class BasePlusCommission looks like? We know that it Will be code reuse since class CommissionEmployee is a direct superclass of BasePlusCommissionEmployee, and the class' Employee (or Person) is an indirect superclass of class BasePlusCommissionEmployee. We know that this is multilevel or hierarchical inheritance and there will be code-reuse, chaining call to the constructor of direct superclass and Indirect superclass. My code doesn't run successfiully. Use private inheritance for convenience and good software engineering please? https://code.sololearn.co

29th Oct 2021, 5:37 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
9 Antworten
+ 1
your code link seems to be truncated name is basic identification here, so these two parameters should be input in all constructors then subclases can call superclass constructor by super( parameters ) the statement has to be in the first line of the constructor You can use more parameters for each constructor or write more constructors with different numbers of parameters or add setters for additional parameters (I prefer this here)
29th Oct 2021, 6:50 AM
zemiak
0
for easy understanding for other sololearn users, we talk about this structure: class Employee String firstName, lastName; class CommissionEmployee extends Employee double grossSales, commissionRate; class BasePlusCommissionEmployee extends CommissionEmployee private double baseSalary;
29th Oct 2021, 6:58 AM
zemiak
0
class Employee{ private String firstName; private String lastName; private String socialSecurityNumber; public Employee(String firstName, String lastName, String socialSecuritynumber) { this.firstName=firstName; this.lastName = lastName; this.socialSecurityNumber = socialSecurityNumber; } public setters and getters firstName, lastName, and socialSecurityNumber; public abstract double earnings(); // or separated interface public String toString() { return String.format(getFirstName(), getLastName(), getSocialSecurity number(), earnings()); } } class CommissionEmployee extends Employee { private double grossSales; private double commissionRate; CommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate) { super(firstName,lastName,socialSecurityNumber) this.grossSales=grossSales; this.commissionRate = commissionRate; } Public setter and Getter grossSales and commissionRate;
29th Oct 2021, 7:51 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
0
public double earnings() { return getGrossSales()*getCommissionRate(); } public String toString() {return String.format(super.getFirstName(),super.getLastName(),super.getSocialSecurityNumber(), getGrossSales() , getCommissionRate()} class BasePlusCommissionEmployee extends CommissionEmployee { private double baseSalary; BasePlusCommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate, double baseSalary). // ???.? Note { super(firstName,lastName, socialSecurityNumber, grossSales, commissionRate); //??? Note this.baseSalary = baseSalary;; } public method setter and getter baseSalary; //Redefine earnings in this //class public double earnings() { return getBaseSalary() + super.earnings(); } // Redefine method toString // in this class public String toString() { return String.format(super getFirstName(), super.getLastName(), super.getSocialSecurityNumber(), super.getGrossSales(), super.getCommissionRate(), earnings()) }
29th Oct 2021, 8:09 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
0
Note: in class BasePlusCommissionEmployee, firstNsme, lastName, socialSecurityNumber are instance variables of Indirect superclass Employee, while commissionRate, grossSales are instance variables of direct superclass CommissionEmployee. Should we use super.super.getFirstNsme()?
29th Oct 2021, 8:13 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
0
Define a package Create classes and interfaces regarding employess Compile all the above files Thats it ! The above steps makes application ever reusable
29th Oct 2021, 9:51 AM
sree harsha
sree harsha - avatar
0
How to define a package in sololearn compiler? Do we must import certain file?
29th Oct 2021, 9:55 AM
Oliver Pasaribu
Oliver Pasaribu - avatar
0
Sololearn doesn't support package
29th Oct 2021, 1:33 PM
zemiak
0
//super.super.getFirstName() is not alowed abstract class Employee ... public Employee(String firstName, String lastName, String socialSecurityNumber) { ... } ... public String toString() { return String.format(""" %s %s, social security number: %s """, getFirstName(), getLastName(), getSocialSecurityNumber() ); } } class CommissionEmployee ... CommissionEmployee( String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate) { super(firstName, lastName, socialSecurityNumber); ... } ... public String toString() { return super.toString() + String.format(""" gross sales: %f commission rate: %f earnings: %f """, getGrossSales(), getCommissionRate(), earnings() ); } } class BasePlusCommissionEmployee ... BasePlusCommissionEmployee( String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate, double baseSalary) { super(firstName, lastName, socialSecurityNumber, grossSales, commissionRate); ... } ... public double earnings() { return getBaseSalary() + super.earnings(); //note super here } public String toString() { return super.toString(); } }
29th Oct 2021, 3:36 PM
zemiak