How can I access methods of different classes in java using the array of type Object -


here code:

public static void main(string args[]) {     circle objcircle = new circle(5);     square objsquare = new square(5);     triangle objtriangle = new triangle(3, 4);     sphere objsphere = new sphere(5);     cube objcube = new cube(5);     tetrahedron objtetra = new tetrahedron(5);     object[] shape = new object[5];     shape[0] = objcircle;     shape[1] = objsquare;     shape[2] = objtriangle;     shape[3] = objsphere;     shape[4] = objcube;     shape[5] = objtetra;     for(int = 0; i<shape.length; i++)     {        //problem occured in section of code         system.out.println(shape[i].getarea());     } } 

}

i have 6 different classes , of them have getarray() method definition of own. want print values getarray() method returns different classes using shape array.

define interface below. implement interface in class circle , square ..

public interface shape {      public double getarea(); } 

then can different classes getarea below.

shape [] shape = new shape [5];     shape[0] = objcircle;     shape[1] = objsquare;     shape[2] = objtriangle;     shape[3] = objsphere;     shape[4] = objcube;     shape[5] = objtetra;     for(int = 0; i<shape.length; i++)     {        //problem occured in section of code         system.out.println(shape[i].getarea());     } 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -