How can I remove a parameter from a subclass constructor in Java?

87
June 29, 2022, at 01:30 AM

In my superclass, I have defined the parameters a, b, c and d. For its subclass, I want to remove d. Is there any way for me to do this?

abstract class Prescription {
    protected Medicine Medicine;
    protected Doctor doctor;
    protected int patientID, thing;
    public Resept(Medicine medicine, Doctor doctor, int patientID, int thing) {
        this.legemiddel = legemiddel;
        this.lege = utskrivendeLege;
        this.pasientID = pasientID;
        this.thing = thing;

And in my subclass, I would like to create a constructor without the last parameter "thing"

public class TypeBPrescription extends Prescription {
    public TypeBPresciption(Medicine medicine, Doctor doctor, int patientID){
        super(Medicine medicine, Doctor doctor, int patientID,)
    }
}

Writing it like this gives me the error that the constructor in the subclass TypeBPrescription is undefined. I want the subclass to not have "thing", but I want my superclass to have it. Is there any way to do this?

Answer 1

I would pass a default value to the super class.

public TypeBPresciption(Medicine medicine, Doctor doctor, int patientID){
    super(medicine, doctor, patientID, 0)
}
Answer 2

Add multiple constructors to the super to clarify thing is optional:

abstract class Prescription {
    private static final int DEFAULT_THING = 0;
    protected Medicine Medicine;
    protected Doctor doctor;
    protected int patientID, thing;
    public Prescription (Medicine medicine, Doctor doctor, int patientID) 
   {
    this(medicine, doctor, paitentId, DEFAULT_THING);

    public Prescription (Medicine medicine, Doctor doctor, int patientID, int thing) {
    this.legemiddel = legemiddel;
    this.lege = utskrivendeLege;
    this.pasientID = pasientID;
    this.thing = thing;
}

Then a sub-class can use whichever constructor fits their context.

Rent Charter Buses Company
READ ALSO
Problem with concurrent sessions and OAuth2 in Spring Boot

Problem with concurrent sessions and OAuth2 in Spring Boot

I'm working with Spring Security 56

114
Pass html data to Quarkus Template

Pass html data to Quarkus Template

I am using Quarkus Mailer and Quarkus Template to create an endpoint that will be responsible just for sending emailsFor now it just receives the subject, body and the emails that the email should be sent to

107
Can I change a keystore JKS file's alias password with only the keystore password and not the alias password?

Can I change a keystore JKS file's alias password with only the keystore password and not the alias password?

Is it possible to change a keystore alias' password without having the oldI do have the keystore's password and the alias name

159
How to deploy apache camel application on wildfly

How to deploy apache camel application on wildfly

I developed an Apache Camel application and I want to deploy it to wildflyFrom this guide https://wildfly-extras

115