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

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -