0

I have this class (let's call it A) and an inherited class (B), and I would like to create a constructor for B using an instance of A, something like :

public B(A a){
   super = a;
   ...
}

Obviously the above code does not work, but is there a way to do it? I could create another instance of A with the same values for each field, but that seems really useless since I already have one, and I just need to add a few fields to make it of class B.

5
  • 2
    Your instance of b could be used as an instance of a, do you really want 2 different objects? Commented Aug 27, 2015 at 13:36
  • 2
    Need more details to propose the best design Commented Aug 27, 2015 at 13:38
  • Are you asking for the concept of copy constructors? Do you want your instance of B to get all the attribute values of A? Commented Aug 27, 2015 at 13:39
  • I'm guessing you want to create an instance of b given an instance of a and a few more values? Commented Aug 27, 2015 at 13:39
  • that's it Kevin (should have specified it) Commented Aug 27, 2015 at 13:56

2 Answers 2

2

You cannot "add" a few fields to an instance of a class to make it of another class. The fields are added to a class, not to an instance of it. So I guess you will have the following options:

  • since B is derived from A your constructor of B will have to receive the information you already send to A(plus some extra fields). Which means you copy the values from your instance of A to the new instance of B
  • if you do not want to copy the values from the instance of A and continue to use that instance, then the best thing to do is to define B not derived from A, and have a field in B of type A plus the extra fields you want.

      public B {
        private A a;
        private int somefield;
        public B( A a, int somefield) {
            this.a = a;
            this.somefield = somefield;
      }
    
Sign up to request clarification or add additional context in comments.

4 Comments

It is a common pattern to extend a class while enclosing an instance of it. For example, a LinkedListNode contains a reference to LinkedListNode next;. Another example, : BufferedWriter takes a Writer in its constructor and decorates its behaviour. It all depends on the context, you cannot say what you say in the general case.
it is true what you are saying, but here it seems that the fields from the parent class need to have the values from instance of A and just add some extra fields. If B would be still be derived from A, and also hold an instance of A, it will have two sets of fields from A, one of its own and one that can access from the instance of A that it refers. And to my understanding is not what is intended.
I don't know the intent of the OP, the question is unclear. Your answer may or may not be relevant
To be a bit more specific, the class A represents an abstract character (with it's name, abilities and so on), and the class B is a character that I can draw, so it only adds methods and fields needed for graphic purposes (texture, skin and so on).
0

I would implement this using some sort of copy constructor:

public class B extends A 
{
    private int field;

    public B(A a, int fieldValue) 
    {
        this.aFieldFromA = a.aFieldFromA;
        this.anotherFieldFromA = a.anotherFieldFromA;
        this.field = fieldValue;
    }
}

6 Comments

I thought about it, but then you would create another instance of A whenever you call the constructor of B. For exemple : A a = new A(1); B b = new B(a, anotherValue); a.firstField = 42; boolean bool = (b.getFirstField()==a.getFirstField());
then bool would be false because the "super" of b is not exactly a, juste another instance of A that has the same field values when the constructor is called. Am I missing something? Because it seems like it should be an easy thing to do. PS : excuse the double post, first time here.
No, because B is a new instance. B and A can not be the same instance, unless you created your A as A a = new B(). Then your a "points" to a B in reality, but as B is derived from A you can use a B for an A.
I get it and that's my point : when you call B's constructor, it will first create an instance of A and then add it a few field to make it an instance of B (at least that's how I view it. It might not be the reality behind the scene, but that's how it feel to me). Why couldn't I create an instance of B using an already existing A and adding the few fields missing to make it a B?
Well, sometimes the answer is just "because that's how it is supposed to be done" ;-) and because what's happening behind the scenes doesn't match what you feel is happening.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.