We know that
String s = new Object();
will result in error:
incompatible types: Object cannot be converted to String
Thus, from the following example, we cannot do:
Car car = new Vehicle();
But what would be wrong for Java having something like this:
Supperclass:
public class Vehicle {
String color;
int numberOfWheels;
int maxSpeed;
// etc.
}
Subclass:
public class Car extends Vehicle {
String engineType;
String transmissionType;
int numberOfDoors;
// etc.
Car(Vehicle vehicle) {
// the theoretical idea of passing
// the superclass object within a subclass
super = vehicle;
}
}
The 'super = vehicle;' would allow us to pass all values of a previously set superclass (vehicle) to new subclasses (car) at one shot. And the usage would be:
public class App {
public static void main(String[] args) {
Vehicle vehicle = new Vehicle();
vehicle.color = "GREEN";
vehicle.maxSpeed = 100;
vehicle.numberOfWheels = 4;
Car car = new Car(vehicle);
// this would print: GREEN
System.out.println("Car color is: " + car.color);
}
}
Perhaps there already is a simple way of doing it similarly.
"Enlighten those who are still in dark ... "