Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7762167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:19:35+00:00 2026-06-01T14:19:35+00:00

I have a question about the use of data structures such as ArrayLists in

  • 0

I have a question about the use of data structures such as ArrayLists in a simple inheritance structure. I’m having a difficult time wording it: hopefully you can understand what I am trying to ask.

I have a Superclass Parrot and a Subclass PirateParrot extending Parrot. In Parrot, I have the following method:

 public String speak() {
     int rand = (int)(Math.random() * sounds.size());
     return sounds.get(rand);
    }

Which returns a random string in an ArrayList called sounds which is created in the Parrot class.

If I create a separate instance of PirateParrot called polly, which also has its own ArrayList, and try to call polly.speak(); without any implicit implementation for the speak method in the PirateParrot class, I get thrown an “Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 0, Size: 0″

If I specifically copy/paste in the speak() method from Parrot into PirateParrot, the code compiles fine and runs properly. What exactly was the problem previously? Is there a way to make this run correctly without having to copy/paste the speak() method into PirateParrot? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:19:36+00:00Added an answer on June 1, 2026 at 2:19 pm

    If I understand the problem correctly, then the simplest fix would be to not declare another sounds variable in PirateParrot. Instead, make sure sounds is declared protected in Parrot, and then in the PirateParrot constructor just populate the inherited sounds variable with whatever sounds you want the PirateParrot to have.

    Another alternative might be to have a getSounds() method that returns the list and call getSounds() from inside of speak() instead of referencing sounds directly. Then PirateParrot would just need to override getSounds() to return its version of sounds.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have in my homework some question about data structure: I have elements which
I have a question about the use of multidimensional std::intializer_list in C++. I have
I have a question about design winforms. Should I use, or not, group boxes
Recently I have asked a question about what I should use to create self-contained
I have question about interpreting strings as packed binary data in C++. In python,
I have a question about how to structure communication between a (new) Firefox extension
People have asked similar questions about the efficiency of various data structures but none
i have some questions about constructors in ColdFusion : must i use the name
I have some questions about partitioning and strategy, how and when to use it.
There have been several questions over the past few days about the proper use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.