java - Polymorphic array isn't working -
this question has answer here:
package finalpaperpractice; public class polymorphictest { public void function(){ system.out.println("1"); } public static void main (string [] args){ object obj [] = {new polymorphictest(), new a(), new b()}; (int = 0; < 3; ++){ obj[i].function(); // error on line } } } class a{ public void function(){ system.out.println("2"); } } class b{ public void function(){ system.out.println("3"); } } i don't know why obj[i] isn't able find function(). have written code overrides tostring() works code doesn't override.
because obj object type , object class doesn't have function method declared.
you can either create interface function method , implement interface in classes wherever want function method.
then,
object obj should change
<your interface> obj
Comments
Post a Comment