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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:45:49+00:00 2026-06-06T13:45:49+00:00

I’m trying to understand what happend when someone casts an object of one specific

  • 0

I’m trying to understand what happend when someone casts an object of one specific class to another class. I.e there are two classes

 public class Aclass{

   private String attribute1;

   private List<String> attribute2;

   //get/set methods

 }

 public class Bclass{

   private String attribute1;

   private List<int> attribute2;

   //get/set methods

 }

Now inside another class i have both these objects and afterwards i cast them.

public class Cclass{

   Aclass aclass=new Aclass();

   //returneddata is a method that returns an Aclass object that contained dta for the Aclass attributes
   aclass=returneddata();

   Bclass bclass=new Bclass();


   bclass=Bclass.class.cast(aclass);

}

From the aforementioned class i’m taking a java.lang.ClassCastException.

The problem is that i have two classes which does not contained only two attributes each but contained both of them 20. The 16 of these atributes are common in both classes.

Moreover the returneddata method returns an object retrieved from a repository. As you can understand i want to find a way and transfer the 16 data attributes of the object Aclass to the object Bclass.

I want:

1) To find a way to rewrite the 16 common attributes data from the one object to the other

2) How the cast is working in general

Any propositions?

  • 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-06T13:45:50+00:00Added an answer on June 6, 2026 at 1:45 pm

    The fact that both classes have similar attributes does not matter. What matters is that Aclass and Bclass are not related to each other: one is not a superclass of the other. If you want to share or move data between these classes, you’ll need to copy the data from one instance to another yourself.

    A casting example

    Let’s say we have an object Fruit fruit = new Banana();. Now if you’d need to call a method of Banana later in the code, you can cast fruit with the cast (Banana). Why? Because you happen to know that fruit is a Banana but the Java compiler does not know that (it thinks it’s a Fruit and it could also be a different fruit). So you have to add the cast to add additional information about fruit and to perform the operation safely.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.