0

I've got a BetterBuilding class which extends Building, and a BetterElevator class which extends Elevator. Building, in its constructor, fills up an array Elevator[] elevators with Elevator objects. Now, I'm trying to create the BetterBuilding constructor as follows:

public BetterBuilding(File f) {
    super(f);
    for(int i=0; i<elevatorNum; i++) {
        elevators[i] = new BetterElevator(20);
    }
    System.out.println(elevators[0].test());
}

So basically, I tried to use the superclass constructor, then change the content of the elevators array to now contain a BetterElevator on position 0. But, when I try to call elevators[0].test(), I get an error saying that elevators[0] is an Elevator (test() is a method only available to BetterElevators). What am I doing wrong? Thanks in advance!

  • Have you tried doing it? Where is your code? A BetterElevator is an Elevator. So you can store a BetterElevator in an array of elevators. – JB Nizet Oct 31 '15 at 19:36
  • I'll edit in some of the class codes. Basically I threw in a method test() in BetterElevator and when I call building.elevators[].test() it says the method test is not recognized for the type Elevator. – Jochem Bruijninckx Oct 31 '15 at 19:41
  • Possible duplicate of [Is it possible to call subclasses' methods on a superclass object?](http://stackoverflow.com/questions/898909/is-it-possible-to-call-subclasses-methods-on-a-superclass-object) – Henry Oct 31 '15 at 20:33
  • Why is test only available to `BetterElevator`? You could have an interface called `Testable` with a test method, then any object you want can implement `Testable`. – OneCricketeer Oct 31 '15 at 21:07

1 Answers1

0

So, your Building class contains an Array of Elevators. Since a BetterElevator is an Elevator (inheritance!) it's no problem to assign BetterElevators to the Elevator-Array.

However, when you access the Array in Building the type of the Array is the common type Elevator, regardless of the actual type of the contained array.

To access methods of BetterElevators you have to first cast it from Elevator.

As there could potentially exist EvenBetterElevators checking the type with instanceof first is advisable or you'll risk ClassCastExceptions at runtime:

if (building.elevators[0] instanceof BetterElevator) {
  ((BetterElevator)elevator).test(); 
}
tom
  • 1,455
  • 1
  • 8
  • 13
  • Thanks, that helps! It does seem a bit superfluous, the way I have it now. Would I have to basically wrap every method in an if/else? So, if(building.elevators[0] instanceof BetterElevator { } else { } To have the method work for both the case that the building has regular/better elevators? Because then I'd have to write the methods twice, I think. Thanks already! – Jochem Bruijninckx Oct 31 '15 at 20:55
  • Not really. Only for Methods that only exist in the subclass and you're working with the type of the superclass. If both super and subclass have the same method `testA` you don't have to cast either because of method overriding. I'd suggest doing a bit of research on Polymorphism/Type Hierarchy/Method Overriding to make matters clearer. – tom Oct 31 '15 at 21:07