Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Really bad at this

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Really bad at this

    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.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Really bad at this

    With this statement:

    a cOb[] = new a[2];

    an array is created that can hold 2 items. And then these statements:

    cOb[0] = new a();
    cOb[1] = new b();
    cOb[2] = new c();

    try to cram 3 items in a sock that can only hold two. From that explanation, you might deduce that an array that holds two items can only have the indices 0 and 1.

    I also recommend:

    In Java, class names begin with capital letters.
    Don't call a class Main - it's just confusing.
    Give your classes and variables better, more descriptive names. Single-letter names, except for loop control variables, are not appropriate.
    Comment your code to describe what it does or is supposed to do.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    Terty_Tree (October 2nd, 2014)

  4. #3
    Junior Member
    Join Date
    Aug 2014
    Location
    South Africa
    Posts
    6
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Really bad at this

    Thank you, I meant to make an array that can hold 5 items but I think I got confused since I only had 2 classes that I wanted it to hold. Appreciate the recommendations, will try my best to implement as much of it as possible. :-)

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Really bad at this

    You're welcome.

    A significant part of becoming better at programming is paying attention to the details and developing your own good practices that when not followed will alert you to small problems before they become big problems.

Similar Threads

  1. Bad Output
    By Fuzzy1450 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 1st, 2014, 01:54 AM
  2. I need help, bad.
    By Mickyy in forum Java Theory & Questions
    Replies: 2
    Last Post: October 24th, 2013, 08:40 AM
  3. BAd Padding exception
    By Bverly in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 26th, 2010, 03:41 PM
  4. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 07:51 PM
  5. Hi, I'm new here. Need help bad!
    By jpechart in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 16th, 2009, 03:03 AM