Home > front end >  Override void method with calculation of private access variable
Override void method with calculation of private access variable

Time:12-01

I am doing an assignment requiring me to create a program to store information on tools. The initial class is used to test the results of other classes. The other 4 are made up of a super class called Equipment and has to be abstract, and then 3 child classes.

I have managed to get the equipment class sorted (I think), but can not work out how to override the replacementYear property which is private as the math to return the result changes for each class.

Math for the battery powered class is Replacement Year = Length of Warranty Purchase Year

Do I need to create an object to complete the math in each class and use a return statement with the replacementYear I am overriding?

Sorry if it not extremely clear but below is the code.

//Parent
abstract class Equipment {
    private String make;
    private String model;
    private int purchaseYear;
    private String replacementYear;
    
    
    //Constructor for Equipment
    public Equipment() 
    {
        make = " ";
        model = " ";
        purchaseYear = 0;
    }
    
    abstract void replacementYear();

public String getMake()
{
    return make;
}

public void setMake(String newMake) {
    this.make = newMake;
}

public String getModel()
{
    return model;
}

public void setModel(String newModel) {
    this.model = newModel;
}

public int getPurchaseYear()
{
    return this.purchaseYear;
}
}

The child class:

class BatteryPoweredEquipment extends Equipment {
    private int warranty = 0;
    
//Constructor
   public void BatteryPoweredEquipment()
   {    }
  
   //set & get warranty
   public int getWarranty()
   {
       return warranty;
   }
   
   public void setWarranty(int newWarranty)
   {
       this.warranty = newWarranty;
   }
   
   //override
    @Override
    void replacementYear() {
        System.out.println(warranty   this.getPurchaseYear());
      }
   
 }

CodePudding user response:

yea, if replacement year needs to computed differently for each type of equipment, then make this method abstract in the abstract superclass so that each extending class can override this to have their custom logic

CodePudding user response:

First of all, you need to create an Equipment constructor that actually accepts all its properties, otherwise, your subclasses would be a pain to use (you would need to call each setter to actually build the object). Talking about setters, that is something you don't really or want (I guess that make, model, purchaseYear do not really change after the Equipment is built and replacementYear should be computed while constructing the object the first time). Additionally, please make use of the new Java Time API (which includes Year and Period instead of modeling years and warranty period as int). After doing this your Equipment class would look as follows:

import java.time.Year;

abstract class Equipment {
    private String make;
    private String model;
    private Year purchaseYear;
    private Year replacementYear;

    //Constructor for Equipment
    public Equipment(String make, String model, Year purchaseYear, Year replacementYear) {
        this.make = make;
        this.model = model;
        this.purchaseYear = purchaseYear;
        this.replacementYear = replacementYear;
    }

    public String getMake() {
        return make;
    }

    public String getModel() {
        return model;
    }

    public Year getPurchaseYear() {
        return this.purchaseYear;
    }

    public Year getReplacementYear() {
        return replacementYear;
    }
}

Now you also need to adjust your child classes. BatteryPoweredEquipment would look as follows:

import java.time.Period;
import java.time.Year;

class BatteryPoweredEquipment extends Equipment {
    private Period warranty;

    public BatteryPoweredEquipment(String make, String model, Year purchaseYear, Period warranty) {
        super(make, model, purchaseYear, purchaseYear.plus(warranty));
        this.warranty = warranty;
    }

    public Period getWarranty() {
        return warranty;
    }
}
  • Related