1

I've 3 classes

Class A{
  int a;
  int b;

  public A(){}

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

Class B extends Class A{
  int c;
  int d;
  public B(){}

  public B(int c, int d){
     this.c = c;
     this.d = d;
  }
}

Class C extends Class B{
  int f;
  public C(int f){
     this.f = f;
  }
}

Now, I'm receiving an object of B(with a, b, c & d values set) from somewhere and f's value as well and I want to create a new instance of C with all the values set. Hence, I created a new constructor in Class C

Class C extends Class B{
    int f;
    C(int f){
         this.f = f;
    }
    C(int f, B bInstance){
      super(bInstance);
      this(f); // because I wanted to only one base constructor
    }
}

Added constructor in B

Class B extends Class A{
  int c;
  int d;
  B(int c, int d){
     this.c = c;
     this.d = d;
  }
  B(B bInstance){
   super(B.getA(), B.getB());
   this(B.getC(), B.getC());
  }
}

Now, this() and super() both needs to be the first statement. Hence, I can't have it this way. I was doing it this way in spirit of keeping only one base constructor and every other constructor calling this one. Here is a link supporting this Best way to handle multiple constructors in Java.

Otherwise, the simple solution would be to have a new constructor in class C and B

Class C extends Class B{
     int f;
     C(int f){
             this.f = f;
     }
     C(int f, B bInstance){
        super(bInstance);
        this.f = f;
     }
}

Class B extends Class A{
  int c;
  int d;
  B(int c, int d){
     this.c = c;
     this.d = d;
  }
  B(B bInstance){
   super(B.getA(), B.getB());
   this.c = c;
   this.d = d;
  }
}

But, I'm trying to learn the best coding standards and wanted to know how to implement it with the best coding practices.

Manjunath H M
  • 818
  • 1
  • 10
  • 25
maxomax
  • 119
  • 5

1 Answers1

1

You are creating a class C which extends B, if I'm understanding correctly, you wish for C to have the values of some class B that you get in some way else where.

When you extend a class you need to call super() on the first line to call the constructor of the super class. So your methods such as in your B class where you have the parameters B(int c, int d) you should have

B(int a, int b, int c, int d) {
    super(a, b);
    this.c = c;
    this.d = d;
}

If you do not do this, you will get a compiler error as the class you're extending has a constructor you're never calling.

I would format it like so...

class A {
    private int a, b;

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

class B extends A {
    private int c, d;

    public B(int a, int b, int c, int d) {
        super(a, b);
        this.c = c;
        this.d = d;
    }
}

class C extends B {
    private int f;

    public C(int a, int b, int c, int d, int f) {
        super(a, b, c, d);
        this.f = f;
    }
}

So, when you instatiate a class C you should perhaps call new C(B.getA(), B.getB() ..etc)

James Andrew
  • 197
  • 1
  • 4
  • 16