-1

I have a superclass that is Abstract and has the following attributes and constructor in it.

public abstract class Equipment {

  public final String serialNumber;

  public final String brand;

  public final String model;

  public final float equipmentPrice;

  public final Integer equipmentStatus;

  public float transactionPrice;

  
  public Equipment(String serialNumber, String brand, String model, float equipmentPrice, Integer equipmentStatus, float transactionPrice) {
  }

}

Child class:

public class Treadmill extends Equipment {

  public double maxSpeed;

  public Treadmill(Integer equipmentStatus, float transactionPrice, double maxSpeed) {
  }

}

Question: in the child class, do I need to include the final attributes that are from the parent class as follows, or should it be the same as the above construct in the child class? Or, should the child constructor hold both the parent and child attributes as follows though most of the parent attributes are final?

public Treadmill(String serialNumber, String brand, String model, float equipmentPrice, Integer equipmentStatus, float transactionPrice, double maxSpeed) {
  }

Final means they can not be changed. If that is the case, how can I have it applied for each child class since the attributes are common for all the different child classes?

Thank you!

  • It doesn't matter where you get the values from, but you do need to call the super constructor to initialize them. – daniu Jan 29 '21 at 22:07
  • Hi @Daniu, thank you for the reply, and can you type the syntax how can I call the super constructor inside the child class? and are you saying that the current child constructor is good since it has all the attributes from the parent and the child class itself? – Adachew Workneh Jan 29 '21 at 22:10
  • 1
    See https://stackoverflow.com/questions/10508107/why-call-super-in-a-constructor – tgdavies Jan 30 '21 at 01:21
  • @tgdavies, thank you for that info but mine is all about the constructor parameters. I have attributes in the abstract parent class and was not sure if I still need to have them in the child constructor as a parameter? Hope that makes sense to you. – Adachew Workneh Jan 30 '21 at 01:40
  • You don't need to have them in the child constructor. You do need to pass a value to the superclass constructor. This will often come from a parameter of the child class constructor, but it can come from anywhere. – tgdavies Jan 30 '21 at 01:43
  • @tgdavies thank you again! There are 3 different child classes of this parent class. the parent class has for instance "model" as one attribute. Each child class will have different model types. How can I pass multiple values (3 different model types - one for each child class) to a single model attribute of the parent class? – Adachew Workneh Jan 30 '21 at 01:52
  • Since each piece of equipment (the subclasses of Equipment) are different classes, you should still only have one modelType for any given piece of equipment. – NomadMaker Jan 30 '21 at 05:00
  • @NomadMaker, that is correct, and thanks for the answer! – Adachew Workneh Jan 30 '21 at 05:34

2 Answers2

0

While all your child classes have the same parent class, this does not mean that an instance of a child class is sharing any of its member fields with any other object.

If we have:

class P {
  final int x;
  P(int x) {
    this.x = x;
  }
}

class A extends P {
  A(int x) {
    super(x);
  }
}

class B extends P {
  B(int x) {
    super(x);
  }
}

Then each instance of A and B have their own field x and each instance can set it to a different value.

You can say:

  ...
  A a = new A(1);
  A aa = new A(2);
  B b = new B(3);
  // and also
  P p = new P(4);
  ...

and each of your objects has a different value for its instance of x.

tgdavies
  • 10,307
  • 4
  • 35
  • 40
  • Thank you @tgdavies and based on your script, the first way of writing is right. If the parent abstract class has the attributes as a final, there is no need to add them in the child constructor as a parameter since the child constructor is able to inherit them and pass its own values. Just a follow-up on this: can the values of the child constructor parameter get overwritten or will it be final? Basically, can I override A(3) by A(10) after its initialization? – Adachew Workneh Jan 30 '21 at 04:19
  • A final field can only be assigned to once, either in an initialiser (e.g. `private final int foo = 1;`) or in a constructor. But I'm not sure whether I understand your question. – tgdavies Jan 30 '21 at 04:23
  • No, you got it, and thanks again for the help! Excellent explanation and example! – Adachew Workneh Jan 30 '21 at 04:30
0

About the part in my comment that you "don't need these parameters in the child constructor": it is possible that a subclass will always use the same value for that variable, in that case it doesn't need it be passed. For example

abstract class GroundVehicle {
  public final int wheelCount;
  protected GroundVehicle(int wheels) {
     wheelCount = wheels;
  }
}
class Car {
  public Car() { // no need for a parameter,
    super(4);    // we always have 4 wheels
  }
}
class Motorbike() {
   public Motorbike() {
     super(2);  // we do need to pass something though
   }
}
daniu
  • 14,137
  • 4
  • 32
  • 53