Monday, February 26, 2018

Java program : Heterogenous Array

class HetrogenousArray
{  
    // USING ARRAY OF 'OBJECT' CLASS WE CAN CREATE AN ARRAY OF HETEROGENOUS ELEMENTS ALSO, BECAUSE 'OBJECT CLASS'
    // IS ROOT CLASS OF ALL CLASES IN JAVA
 
    public static void main(String[] args)
    { Object[] o=new Object[3];
       o[0]=new Emp(111,"Ashutosh");
       o[1]=new Student(222,"Yash");
       o[2]=new Integer(19);
      
       for(Object oo:o)
       { if(oo instanceof Emp)
        {    Emp e =(Emp)oo;
            System.out.println(e.id+"---"+e.name);
        }
       
         if(oo instanceof Student)
        {    Student s =(Student)oo;
            System.out.println(s.id+"---"+s.name);
        }
         if(oo instanceof Integer)
        {   
            System.out.println(oo);
        }
       }    
    }   
}

Thanks
Happy Computing!

No comments:

Post a Comment

How can I run a C++ program directly from Windows?

How-can-I-run-a-C-program-directly-from-Windows