Felix
Felix

Reputation: 1810

Java inheritance - could passing superclass object into a subclass be simplified?

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 ... "

Upvotes: 5

Views: 2288

Answers (5)

Mahdi Rajabi
Mahdi Rajabi

Reputation: 602

Since you declare Vehicle as non-abstract class, JVM look at it as instantiable class.

super and this are references with particular meaning in Object oriented languages. If you were familiar with Object Oriented Compilers and runtime environments, you would be get why you cannot access derived classes.

Imagine you you have to Concrete classes which inherit Vehicle, so which concrete class you want to refer?

Another issue, Compiler take space in heap according to the pattern, it Collect all information (Attributes and behaviors) as a single building block for objects, override Attributes and reserve space for all methods, this reference means all methods which are solidly defined in concert class and non-overriden methods of super class, (Super class is only concept in development, at run time it's space will merge with it's concert class), super reference means all methods which are overriden by it's subclasses but still the method's code space is addressed by the building block.

by these simple issues you will find out that changing super reference or this reference is impossible and will round the OOP Concepts.

Upvotes: 0

JohanLarsson
JohanLarsson

Reputation: 195

Doing that would not be good as any methods called on car would be the methods found in the baseclass, or atleast that is what happens in the normal case when it is the otherway around. It's the object's methods that gets called, not the variable's.

Upvotes: 0

Blasanka
Blasanka

Reputation: 22437

Thus, from the following example, we cannot do:

Car car = new Vehicle();

Yeah, you cannot do that because Vehicle is the parent of Car. So, you can do:

Vehicle car = new Car();

That is part of polymorphism. And the simple way to do what you want to do is,

First add constructor to Vehicle class,

public Vehicle(String color, int numberOfWheels, int maxSpeed) {
     this.color = color;
     //other two assignment
}

Then in Car class constructor,

public Car(String color, int numberOfWheels, int maxSpeed, String engineType, String transmissionType, int numberOfDoors) {
    super(color, numberOfWheels, maxSpeed);
    this.engineType = engineType;
    this.transmissionType = transmissionType;
    this.numberOfDoors = numberOfDoors;
}//keep remember, you can also use constructor overloading.

Now inside main(),

Vehicle car = new Car(/*arguments*/);

Upvotes: 3

Noman Khan
Noman Khan

Reputation: 960

You can do similar as

public Car(Vehicle v){
 super(v)
// assuming super has constructor to copy from Vehicle
}

Upvotes: 0

T.J. Crowder
T.J. Crowder

Reputation: 1074485

You can do something similar to that, but you still need the provide the Car-specific information (either as arguments to the Car constructor, or defaults, or a combination of both).

One fairly common way is to define a copy-constructor for Vehicle in Vehicle:

public Vehicle(Vehicle other) {
    this.color = other.color;
    this.numberOfWheels = other.numberOfWheels;
    this.maxSpeed = other.maxSpeed;
}

Then in Car, you use that copy-constructor and then flesh out the Car details:

public Car(Vehicle v/*, and possibly car-specified args...*/) {
    super(v);
    // ...fill in `Car`-specific information
}

Upvotes: 3

Related Questions