Home > Blockchain >  Java Subclass Doesn't Inherit its Parent's Variable
Java Subclass Doesn't Inherit its Parent's Variable

Time:10-08

I've been working with my project and I having a problem inheriting parent class' variable. Here's my code:

class function3 {

    double getTh;
    double getTt;
    double getOt;
    
    public void display(String name, String id, double getTh, double getTt, double getOt) {

        this.getTh = getTh;
        this.getTt = getTt;
        this.getOt = getOt;
        
        System.out.println("SALARY INFORMATION");
        System.out.println("-----------------------");
        System.out.println("Employee Id       : "   name);
        System.out.println("Employee Name     : "   id);
        System.out.println("Number of hours   : "   getTh);
        System.out.println("Employee Tardiness: "   getTt);
        System.out.println("-----------------------");

        function4 displayTotal = new function4();
        displayTotal.calcSalary(getOt);
        displayTotal.displaySalary();
    }
    
    
}
class function4 extends function3 {

    double overtimePay;
    double weeklyIncome;
    
    public void calcSalary(double getOvertime) {

        overtimePay = getOvertime * 75.25;
        weeklyIncome = getTh * 200.50;
        
        System.out.println(weeklyIncome);
        
    }

    public void displaySalary() {

        System.out.println("WEEEKLY SALARY");
        System.out.println("-----------------------");
        System.out.println("Overtime Pay : "   overtimePay);
        System.out.println("Weekly Income: "   weeklyIncome);
        System.out.println("Gross Income : ");

    }
}

Now my problem is, I can't access the getTh variable from function3 even if it's already extended. What should I do?

CodePudding user response:

Subclasses cannot know, at runtime, about other instances of its parent class through inheritance alone

Subclasses have their own variables that get inherited, but this is only the attributes themselves, not any defined runtime values.

If you want access to another instance, you need composition, not inheritance

class function4 extends function3 {  // maybe not needed to extend anymore 
    public function4(function3 f) {
        this.f3 = f;
   } 
    function3 f3;

Then access this.f3.getTh

CodePudding user response:

What happens if you declare the variables public? By default in Java, variables are defined as package-private which means they are only visible to other classes in the same package, depending on how you have your project setup the 2 classes (function3 and function4) may be in different packages.

Also, as you progress through school (or learn on your own) you will discover that allowing a class to directly manipulate the variables in another can cause issues. S.O.L.I.D. is one of the current methodologies for designing useable and maintainable code. the O in solid means (among other things) that the instance variables of a class should not be able to be accessed by functions outside of the class. Instead you should design accessors and mutators (getters and setters), here is an example:

public class function 3 {

  private double Th;
  private double Tt;
  private double Ot;

  public double getTh() { return Th; }
  public void setTh(double newTh) { Th = newTh; }

  public double getTt() { return Tt; }
  public void setTt(double newTt) { Tt = newTt; }

  public double getOt() { return Ot; }
  public void setOt(double newOt) { Th = newOt; }

}

Then if you want to get the value of Th you can call <instanceOfFunction3>.getTh()

  • Related