benzabill
benzabill

Reputation: 259

Returning subclass instead of class

In this situation, do I have access to the overridden/additional methods of CarSubClass?

public Car getCar(){
CarSubClass carSub = new CarSubClass();
return carSub;
}

Update:

Car has subclasses SubClass1, SubClass2, SubClass3.

getCar() can return SubClass1,SubClass2, OR SubClass3.

Can I do this?:

Car car = getCar();

switch(car.getType()){//getType() returns a byte

case SubClass1:

SubClass1 car1 = (SubClass1)car;
break;

case SubClass2:
SubClass car2 = (SubClass2)car;
break;

case SubClass3:
SubClass3 car3  =(SubClass3)car;
break;

default: //don't do anything

}

Upvotes: 0

Views: 128

Answers (3)

AllTooSir
AllTooSir

Reputation: 49432

Only overriden methods :

Car car  = getCar();
car.method(); // here you can invoke only the overridden methods in CarSubClass

You cannot call the additional methods of CarSubClass which the reference type Car has no knowledge of. It will fail during compilation itself.

Probably this is possible , but you must be certain what you are doing here :

CarSubClass c = (CarSubClass)getCar();
c.subClassSpecificMethod();

The above casting is safe in your case because the method always returns an instance of CarSubClass . It is better to perform a check though.

Car c = getCar();
if(c instanceof CarSubClass){
    ((CarSubClass)c).subClassSpecificMethod();
}

switch(c.getType())

That is not a valid switch key.

JLS§14.11:

The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type (§8.9), or a compile-time error occurs.

Upvotes: 1

H.Rabiee
H.Rabiee

Reputation: 4847

Yes, through dynamic typing/calls it will be available. This is the whole purpose of polymorphism.

But since you are returning a Car instance, whichever class that calls 'getCar()' must cast it to a 'CarSubClass' first.

Car.java

public class Car {

public void a() {
    System.out.println("In Car");
}

}

CarSubClass.java

enter code herepublic class CarSubClass extends Car {

@Override
public void a() {
    System.out.println("In CarSubClass");
}

public static void main(String[] args) {
    Car c = new CarSubClass();
    c.a();
}

}

Will output:

'In CarSubClass'

Edit: I have edited my answer. This whole concept is called polymorphism. You are not required to cast, since at runtime, the dynamic type the variable holds, will ensure that the correct method is called.

Upvotes: 1

STM
STM

Reputation: 36

Not unless you cast it to CarSubClass. See below as an example (a really bad one though):

public class Test {

    public static Car getCar() {
        return new SmallCar();
    }

    public static void main(String[] args) throws Exception {
        ((SmallCar) getCar()).getMake();
    }

    static class Car {

        String model;

        public String getModel() {
            return this.model;
        }
    }

    static class SmallCar extends Car {

        String make;

        public String getMake() {
            return this.make;
        }
    }
}

Best of luck...

Upvotes: 0

Related Questions