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

The Archive Base Latest Questions

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

I want to pass an instance of a super class to a constructor of

  • 0

I want to pass an instance of a super class to a constructor of a sub class. My first idea was to swap the instance of the super class in the sub class similar to javascripts prototypes, but I was told here that Java does not support swapping the reference of the super instance because there is no super instance per se.

To circumvent this issue I want to use a copy constructur which accepts a super class instance. Then I will have to relink all references manually which on the long run will invite bugs when other people extend the code of the super class and forget the copy constructur in the sub class.

Question: Is there some neat and nice way to copy all references automatically, maybe with some reflection mechanism to prevent future bugs?

  • 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-27T18:49:40+00:00Added an answer on May 27, 2026 at 6:49 pm

    You shouldn’t copy all the references from the superclass instance to the subclass instance. BTW, all these references should not even be accessible from the subclass, if they are declared as private fields in the superclass (as they should be, generally).

    What you probably need is delegation, instead of inheritance:

    // Foo is the superclass
    private class FooAdapter {
        private Foo foo;
    
        public FooAdapter(Foo foo) {
            this.foo = foo;
        }
    
        public void doThis() {
            return foo.doThis();
        }
    }
    

    FooAdapter could extend Foo or (better) theyr should implement a common interface, but that’s not necessarily needed.

    If this doesn’t answer your problem, please tell us what you want to do, instead of telling us how you want to do it. What’s the problem you want to solve?

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

Sidebar

Related Questions

I want pass a class-instace to a method of an other and be sure
I want to pass through configuration arguments to a class. These are all the
I want to pass a default argument to an instance method using the value
I need to pass an instance variable (self.rank) to be used by a class
I want to pass some data around threads but want to refrain from using
I want to pass YML-like config data to the scenario of cucumber. for instance:
I want to pass a few variables to another page. Currently I'm using response.redirect
i want to pass large variables to a PHP script on another server with
I want to pass a variable to he buttonEvent method in the selector. [button
I want to pass an array of arbitrary struct pointers and a comparison function

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.