Home > Mobile >  How do I make the daughter class constructors? Relating to parent and daughter inheritance in Java
How do I make the daughter class constructors? Relating to parent and daughter inheritance in Java

Time:10-23

So, not so much a question for a specific project but more for understanding for future.

I don't understand what the daughter class constructors are meant to look like and contain. I understand that the parent class will initialise their attribute and objects of other classes if need be, but when it comes to the daughter class do you initialise the parent class attributes you want to use through the super() function as well as their own attributes?

For example:

public class A{
  private int num1;
  private int num2;

  public A (int num1, int num2){
    this.num1 = num1;
    this.num2 = num2;
  }
}

class B extends A{
  private int num3;
  private int num4;

  public B (int num1, int num2, int num3, int num4){
    super(num1, num2);

    this.num3 = num3;
    this.num4 = num4;
  }
}

class C extends A{
  private int num5;
  private int num6;

  public C (int num1, int num2, int num5, int num6){
    super(num1, num2);

    this.num5 = num5;
    this.num6 = num6;
  }
}

(Not running code... (I don't think, I'm new to Java)

From my understanding, in both daughter classes i need to initialise the parent classes attributes if I want to access them and then use the whole get and set shebang and then super.method to use them. Where I'm confused is the fact that is this the right way of going about it? Is this completely wrong or am I somewhat on the right track?

CodePudding user response:

I think this is not far off the mark. If you add get/set for num1 and num1 in class A and make them public, you can call them on the subclasses directly as they are inherited. I.e.

public class A {
  // same as above, but add:
  public int getNum1() {
    return num1;
  }

B b = new B(1,2,3,4);
int x = b.getNum1();

would work.

CodePudding user response:

You are on the right track, but the example is not that good.

First of all, in Java, even without declaring it and if there are no other constructors, you will get the default one which has no arguments.

Therefore when you go for a B extends A the constructor will call super(); by default without needing you to manually write it, only if you want to in order to specify some arguments because of the parent class.

Then, to better understand the inheritance and any other topic in the field, try to give some context or meaning to your examples, like Circle, Rectangle, Square which is extending a Shape class. This will help you a lot in the future.

Finally, you cannot have a child class without its parent, but you can have a parent without children. Keeping this in mind will ease the overall understanding of the inheritance.

Another viable example:

public class A {
  private int num1;
  private int num2;

   public A () { // this will be called with super() method call from B's constructor

  }

  public A (int num1, int num2){
    this.num1 = num1;
    this.num2 = num2;
  }
}

class B extends A {
  private int num3;
  private int num4;

  public B (int num3, int num4){

     // super(); // called by default, but you can write it

    this.num3 = num3;
    this.num4 = num4;
  
  }
}
  • Related