0

I have a class C with lots of members(say 10 members) + members that are only in C

I have two more classes A and B which has exactly those above mentioned 10 members. (A and B each does not have those 10 members. Those 10 members are distributed among A and B and A and B has few other fields)

class C {
    private int member1;
    private String member2;
    private String member3;
    private float member4;
    ...
    private String member10;

    private member11_only_in_C;
    private member12_only_in_C;
    ...
}



class A {
    private String memeber10;
    private float member4;
    private double member6;
    ....
}
class B {
     private int memeber1;
     private String memeber2;
     private String member7;
     ....
 }

So when I have cObject I need to set the values of all members in A and B like

aObject.setMember10(cObject.getMember10());
bObject.setMember1(cObject.getMember1());
... and so on for all 10 members

This looks bad(long and in future If i add more members to C,A,B I need to write setters for them). So what I thought was If I have class C extend or implement A and B I can cast it like

 A aObject = (A)cObject;
 B bObject = (B)cObject;

But the problems are

  1. Java does not allow multiple inheritance
  2. I cannot make A and B as interfaces because I need to set their values. (If I make them as interfaces, their members would become final, which means that I cannot change their values)
  3. I cannot extend one class and implement other. (I then cannot set values of members of the interface)

What can I do now?

Thanks..

Exuberant
  • 189
  • 2
  • 13
  • 1
    You've said the similarities between `A`, `B` and `C` (they all have the same fields), but what are the differences? Why do you need 3 classes rather than 1? – Paul Boddington Sep 11 '15 at 18:28
  • A and B have members that are in C. C can have any other fields. – Exuberant Sep 11 '15 at 18:31
  • 1
    This still reeks of bad code design. You should have a base class with only common members, and use aggregation and inheritance (or some other non-class-based methodology) to incorporate the additional members into A, B, and C. – Shotgun Ninja Sep 11 '15 at 18:34
  • As @ShotgunNinja stated, proper design would be something like this: if C is a dump truck, B might be a truck, and A might be a motor vehicle, so A is the super class of B and B is the super class of C. There's nothing to prevent a class D, say a car from also having A as it's super class. Your casting code will still work just fine. – John Kuhns Sep 11 '15 at 18:35
  • If you can't change the hierarchy of A, B, and C, then you should consider encapsulation of A and B inside of C, and route C's accessors to use A or B to provide the information needed. This can be sloppy and excessive, however, and may detract from the ability of users to understand where their information is coming from. – Shotgun Ninja Sep 11 '15 at 18:42
  • I agree that design is a bit sloppy. But when I have an object of class C, I need to set all values of A and B and also compute values of some fields in A and B based on certain values of fields of C – Exuberant Sep 11 '15 at 18:48

2 Answers2

1

As I understand it you have two classes A and B, both with a number of fields, and you want to create a third class C with all the fields of A and B.

As you point out you can't do this with inheritance in Java because multiple inheritance of state is not supported.

You can do something similar using composition instead.

public final class C {

    private final A a;
    private final B b;

    public C(A a, B b) {
        this.a = a;
        this.b = b;
    }

    ...

    void setMember7(String s) {
        b.setMember7(s);
    }

    String getMember7() {
        return b.getMember7();
    }

    ...
}

I'm not sure if this is a good idea or what you need though. We would need more information about your use-case.

EDIT

If A and B have fields that are not in C you could make a class A2 with all the fields common to A and C and a class B2 with all the fields common to B and C, like this:

public final class A {

    private final A2 a2;
    // other fields

    public A(A2 a2) { 
        this.a2 = a2;
    }
}

public final class B {

    private final B2 b2;
    // other fields

    public B(B2 b2) {
        this.b2 = b2;
    }
}

public final class C {

    private final A2 a2 = new A2();
    private final B2 b2 = new B2();

    ...

    public void setMember7(String s) {
        b2.setMember7(s);
    }

    public String getMember7() {
        return b2.getMember7();
    }

    ...

    public A getA() {
        return new A(a2);
    }

    public B getB() {
        return new B(b2);
    } 
}
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • I have all three class to begin with. When I have an object of class C I need to populate the fields of A and B – Exuberant Sep 11 '15 at 18:39
  • This does not solve my problem. Still all members have to be set one by one – Exuberant Sep 11 '15 at 18:54
  • @Ashwin I've changed my answer. I don't know if this is any better. You can avoid setting fields one by one by bundling fields up into a class. – Paul Boddington Sep 11 '15 at 19:02
  • @Ashwin You don't. Whatever methods you currently have for `C` don't need to change - the only difference is that they change fields of `A2` and `B2` rather than fields of `C` directly. If you don't want to write setters, you can simply make the fields of `A2` and `B2` package private (not usually a good idea but it works). If you don't want to use composition, and you don't want to transfer the fields individually, you would have to resort to reflection - read through the fields of `A` in a loop and set the values according to values of a `C` instance, but that would be hideous. – Paul Boddington Sep 11 '15 at 19:12
0

I don't know exactly what you want but i think what you need can be accomplished in the constructor

Class A{
 private member1;
 private member10;
 Class A(){
 }
 Class A(C cObject){
  member1 = cObject.getMember1();
  //the rest of member varaibles you want to set
  member10 = cObject.getMember10();
  }
}
Class B{
 private member1;
 private member10;
 Class B(){
 }
 Class B(C cObject){
  member1 = cObject.getMember1();
  //the rest of member varaibles you want to set
  member10 = cObject.getMember10();
  }
}

to use it :-

 B objectB = new B(objectC);
 A objectB = new A(objectC); 
karim mohsen
  • 2,164
  • 1
  • 15
  • 19