I've got an external class I cannot modify and I want to add customized functionality to that class related to my project, so I created a class which extends it in order to add the extra methods I need.
The problem is that Car class doesn't have a constructor, so I can't use super, the only way to create a Car object is through a native method that returns a fully filled Car object.
public class Car {
//The only way to create a car
public native Car readFromFile (String file);
//Many other variables and methods
}
And my custom class:
import com.example.Car;
public class CustomCar extends Car {
public void extraMethod1(){
//do something using Car variables
}
}
Obviously I cannot cast to (CustomCar) as I have ClassCastException, so how can I get a Car from that native method and use on that object the methods from CustomCar?
Should I just have a variable Car in CustomCar and use it in CustomCar methods?
import com.example.Car;
public class CustomCar {
Car car;
public void extraMethod1(){
car.doSomething(); //do something using Car variables
}
}
Or should I just create a new constructor copying all the parents variables?
Many thanks for your help!