4

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

Felix
  • 1,662
  • 2
  • 18
  • 37
  • Is your question, regarding why Java does not provide it by default? Or you are trying to figure how to implement such a behavior for your problem – gpr Aug 18 '18 at 08:20
  • Good question. I must admit - both - as long as the answer to how to do it is similar in simplicity to the idea. And, if you can provide a true explanation or reason why Java cannot do this, I surely am interested too - a link is fine. – Felix Aug 18 '18 at 08:33
  • @Felix - *"if you can provide a true explanation or reason why Java cannot do this"* Java **could** do this (if by "this" we mean copying all instance data from `vehicle` to the current object when you do `super = vehicle`). It just doesn't. – T.J. Crowder Aug 18 '18 at 08:59

5 Answers5

3

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
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I understand, but we are still coding the individual fields of the superclass. The example is simple, but in real life there are classes full of fields where the simple 'super = subclass' would go long way (save lots of coding). Don't you think? – Felix Aug 18 '18 at 08:23
  • Please look at https://stackoverflow.com/questions/827785/why-doesnt-java-have-a-copy-constructor – gpr Aug 18 '18 at 08:25
  • @Felix - What I think doesn't really matter. :-) `super = x` isn't something Java provides, you do need to specify the fields when creating the copy constructor (or use reflection, but that's a big hammer). If you want to to suggest an enhancement to Java to provide syntax like that, you could create a JSR and try to get it through the [JCP](https://en.wikipedia.org/wiki/Java_Community_Process). – T.J. Crowder Aug 18 '18 at 08:27
  • 1
    @Felix I understand there may be a lot of field to be copied, but it doesn't mean you have to write it. there are tools in every good IDE which can generate that code for you. – Ali Aug 18 '18 at 09:48
2

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*/);
Blasanka
  • 21,001
  • 12
  • 102
  • 104
0

You can do similar as

public Car(Vehicle v){
 super(v)
// assuming super has constructor to copy from Vehicle
}
Noman Khan
  • 920
  • 5
  • 12
0

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.

JohanLarsson
  • 195
  • 1
  • 7
  • No, it would not. We would be passing object of the same class to its own class inside a subclass. All variables and methods would belong to the given superclass only. The subclass would retain its own as well. No conflicts. – Felix Aug 18 '18 at 09:36
0

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.

Mahdi Rajabi
  • 582
  • 1
  • 4
  • 11