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

  • Home
  • SEARCH
  • 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 7401407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:27:39+00:00 2026-05-29T04:27:39+00:00

Suppose I have a superclass Item and a subclass MovingItem. If I create an

  • 0

Suppose I have a superclass Item and a subclass MovingItem.

If I create an array of items and then try to cast one of the already created items into a MovingItem and store it in to a vector, does it mean I use a reference or I create a new object, for instance.

Item[] itms = new Item[2];
itms[0] = new Item();
itms[1] = new Item();

Vector<MovingItem> movingItms = new Vector<MovingItem>();
movingItms.add((MovingItem) itms[0]);

What happens when I cast the object of type Itm found in array itms at index 0 and then store it in to the vector? Do I store a reference or casting creates a new object of type MovingItem and then adds it to the vector.

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-05-29T04:27:40+00:00Added an answer on May 29, 2026 at 4:27 am

    Your example will throw a ClassCastException. You can cast or assign object only to its actual type or any of its supertypes, but not to its subtype.

    Item i = new MovingItem(); // ok, because a MovingItem is an Item
                               // a cast is possible, but redundant in this place
    MovingItem mi = new Item(); // won't compile, an Item isn't a MovingItem
    MovingItem mi = (MovingItem) new Item(); // throws ClassCastException
                                             // an Item isn't a MovingItem
    

    Of course, casting an object doesn’t create a new object, it only creates a new reference to it (if the cast doesn’t fail with a ClassCastException as above).

    Object o = new Integer(5); // assign an Integer instance to an Object variable
    Integer i = (Integer) o; // cast and assign to a new Integer variable
                             // still points the the same actual instance
    assert i == o; // true, these two variables point to the same Integer instance
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have BaseClass with public methods A and B, and I create DerivedClass
Suppose I have MyEdge and MyEdgeModified , such that MyEdge is the superclass and
Basically I'm having a little issue here. I have a superclass and a subclass.
Suppose you have an array value => timestamp. The values are increasing with the
I have a class with a subclass. The superclass has a Position property. The
Suppose I have 3 completely different layouts for one site. The first is shown
Suppose I have a superclass Entity and some subclasses Creature , Heroes etc. I
I have a common superclass (suppose it's Employee ), which has (amongst others) subclasses
Suppose you have 2 different ASP.NET applications in IIS. Also, you have some ASCX
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new

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.