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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:18:00+00:00 2026-05-24T07:18:00+00:00

Consider the following case: class A { int x; int y; } class B

  • 0

Consider the following case:

class A {
  int x;
  int y;
}

class B extends A {
  int z;
}

Now, somewhere in the code this classes are used like this:

A objA = getAFromSomewhere();
B objB = null;

And in a certain situation I want to do something like

objB = objA; // can't do this
objB.z = someZ;

Of course the real objects are a bit more complicated, so it’s not just about copying two ints. But they aren’t overly complex either.

I know I can write a constructor for B like this:

public B(A anA) {
  this.a = anA.a;
  this.b = anA.b;

  this.z = 0;
}

But if that’s really the only way, I prefer merging the additional members of B into A.

update considering the answers

My question was not clear enough. I understand that objB = objA; can’t work (thus I asked for “something like”, meaning something with comparable code complexity) and I know about the issues with shallow vs deep copies.
What I was looking for is a possibility to copy the members of a base class (let’s say using clone()). You may understand that copying every member manually is a bad solution as it adds complexity and redundancy to the code. Thanks for your replies anyway!

  • 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-24T07:18:01+00:00Added an answer on May 24, 2026 at 7:18 am

    There’s no trivial solution to this because there’s no one-size-fits-all solution. Basically you don’t have all the information within a B, so you can’t guarantee you would have a "sensible" B object.

    You probably just want to create a constructor in B which takes an A and copies all the A data into the new B.

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

Sidebar

Related Questions

Consider the following: object Main { case class Foo(bar: Int) extends FooList { val
Consider the following piece of code: case class User(id: Int, name: String) object User{
Please consider the following simple use case: public class Foo { public virtual int
Consider the following test case: public class Main { static int a = 0;
Consider the following Scala case class: case class WideLoad(a: String, b: Int, c: Float,
Consider the following code: #include <iostream> using namespace std; class Test { static int
Please consider the following code: <?php class MyException extends Exception {} function global_exception_handler($exception) {
Consider the following simple code import java.util.*; public class MainTest<T extends Object1<?,?>> { List<T>
please consider following code #include <iostream> using namespace std; class Digit { private: int
Consider the following case: public class A { public A() { b = 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.