lundi 30 octobre 2017

Java: How can I instantiate a parent object from child without using parent constructor?

Here's the example:

public class Car{
  private float param;
  public Car(float rawParam) {
    // process rawParam before assign it to this.param
    this.param = rawParam * 2;
  }
}

public class Truck extends Car{
  public Truck(Car car) {
    // How do I instantiate Car?
  }
}

There's no default constructor, so I can't just create some arbitrary parent object then clone the passed car by reflection. The only constructor provided needs some raw params which are unknown to the parent object itself. How can I instantiate Car and Truck in this case? Thanks!





Aucun commentaire:

Enregistrer un commentaire