-3

Hy,

Please help me with the folowing polymorphic example:

    interface Car {}
    class Toyota implements Car {}
    class ToyotaSUV extends Toyota {}
    class Drive {
public static void main(String [] args){
          Car c = new Car();
          Toyota t = new ToyotaSUV();
          Car s = new ToyotaSuv();

}

Which of the above can and can't be instantiated?

ToyotaSUV can be a Toyota or/and Car?

Sincerely,

SocketM
  • 564
  • 1
  • 19
  • 34

1 Answers1

1
  1. You can't instantaite an interface, that is, you can't instantiate "Car". Interfaces can't be instantiated because they don't have a constructor.
  2. ToyotaSUV IS a Toyota AND can behave as a Car. By the moment you extend ToyotaSUV you are saying that ToyotaSuv IS a Toyota and can do exaclty the same things that Toyota can. Also, as Toyota can behave as a Car beacause it implement that interface, you are saying that ToyotaSUV can behave as a Car too, beceause it also inheritance those methods.