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 8616307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:35:13+00:00 2026-06-12T05:35:13+00:00

I have strange problem. I have three classes. Book ( abstract ), and two

  • 0

I have strange problem. I have three classes. Book ( abstract ), and two subclasses ( Fiction, Nonfiction). I have created array of Book references. That array can hold both subclasse’s objects. I’have following code for Fiction class.

public class Fiction extends Book{

public Fiction(String title) {
    super(title);

    setPrice();
}

public void setPrice() {

    super.price = 24.99;

}

}

And the “same” code for Nonfiction class

public class Nonfiction extends Book{

public Nonfiction(String title) {
    super(title);

    setPrice();
}

public void setPrice() {

    super.price = 37.99;

}

}

And this is Main program.

import javax.swing.JOptionPane;

public class Main {

public static void main(String[] args) {

    Book[] book = new Fiction[6];

    for(int i = 0; i<book.length; i++) {

        if(i<4) {
            String title = JOptionPane.showInputDialog("Enter fiction book, no "+i);
            book[i] = new Fiction(title);

        } else {

            String title = JOptionPane.showInputDialog("Enter non fiction book, no "+i);
            book[i] = new Nonfiction(title);

        }

    }

    for(int x = 0; x<book.length; x++) {

        System.out.println("Book named "+book[x].getTitle()+" costs "+book[x].getPrice());

    }


}

}

For any reason i’m getting ArrayStoreException on this line..

book[i] = new Nonfiction(title);

But same code works for Fiction class. I checked java documentation and it says that this exception is thrown when i’m trying to store different objects in array ( like String and Int ), but in this case they are all the same objects. Both classes are children of superclass, that means, they are of the same type. I’m confsued. A little help here please?

  • 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-12T05:35:14+00:00Added an answer on June 12, 2026 at 5:35 am

    Replace this: –

    Book[] book = new Fiction[6];
    

    With : –

    Book[] book = new Book[6];
    

    By using first way of declaration, you are actually telling compiler that Array will contain object of type Fiction and not any other subtype of Book

    You have confused yourself between: –

    Book book = new Fiction();
    

    and

     Book[] books = new Fiction[6];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange problem getting the color of the point that was touched.
This is a strange problem. I have a batch file where I have two
Strange and probably trivial problem. I have three projects in one solution (.NET 2.0,
I have a strange problem in my Core Data app. I have three entities
Have a strange problem in that I have a Rake task that seems to
I have a problem so strange that I hardly can put an adequate title
I have a strange problem with task scheduler. Here are three tested configurations of
i have strange problem doing reporting: i have numerous clients with different issued invoices.
I have strange problem with receiving data from socket. On client im using air
I have a strange problem on ipv6 connection. I write down a simple client

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.