No idea what is happening and why it's not working. New to the concept of polymorphism.
public class main{ public static void main(String[] args){ a cOb[] = new a[2]; cOb[0] = new a(); cOb[1] = new b(); cOb[2] = new c(); for(int x = 0; x < cOb.length; x++){ cOb[x].master(); } } } public class a{ public void master(){ System.out.println("I'm the master."); } } public class b extends a{ public void master(){ System.out.println("I might be the master."); } } public class c extends a{ public void master(){ System.out.println("I'm probably not the master."); } }
Whenever I run the code it gives me the following error:
"Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
at average.main.main(main.java:9)"
My guess was that it was something that was wrong with line 2 or 9 in the main class but I couldn't find any mistake.
Thanks in advance for any help.