MatiasMerkuri
12/29/2018 - 3:21 PM

Polymorphic Array

POLYMORPHIC ARRAY WHICH LOOPS THROUGH DIFFERENT CLASS OBJECTS AND CALLS A METHOD

class First{
    public static void main(String args[]){

        Fourth matias[] = new Fourth[3];
        matias[0] = new Second();
        matias[1] = new Third();
        matias[2] = new Fourth();

        for (int x= 0; x < matias.length; x++){
            matias[x].eat();
        }
    }
}
public class Second extends Fourth{

    void eat(){
        System.out.println("This is the coolest.");
    }
}
public class Third extends Fourth{

    void eat(){
        System.out.println("This is cooler.");
    }
}
public class Fourth{

    void eat(){
        System.out.println("This is cool.");
    }
}