'calling construtor of super class and an overloaded constructor of the subclass within the subclass constructor error in java

i can't call superclass constructor nor can i call another overloaded constructor so is there any way to initializing the data of the two class without hardcoding

i know that i can only call one constructor either the subclass or the superclass constructor within the constructor and this should be the first statement in the constructor body but is there a better way to initialize the the data without writing a redundant code?

if there is no way to do this does that implies that i should call the superclass constructor(to be able to initialize its private data) and then initialize the data of the subclass explicitly?

thanks in advance!

private double side1;
private double side2; 
private double side3; 

public Triangle(){
    this.side1 = 1;
    this.side2 = 1;
    this.side3 = 1; 
}
public Triangle(double side1, double side2, double side3, String color, boolean filled) {
    this(side1, side2, side3);
    this(color,filled); //error
}
public Triangle(String color, boolean filled) {
    super(color,filled); //
}
public Triangle(double side1, double side2, double side3){
    setSide1(side1);
    setSide2(side2);
    setSide3(side3);
}[enter image description here][1]


Solution 1:[1]

You can only call super() or this() once per constructor declared, and eventually super() will be called. If you're calling a constructor overload (this()), then the constructor it calls will invariably have to call either this() (another constructor), or super() (finally constructing the object).

That said, for your use-case, a simplification could be:

public static final double DEFAULT_SIDE_LENGTH = 1;
public static final String DEFAULT_COLOR = "Red";
public static final boolean DEFAULT_FILLED = true;

public Triangle() {
    this(DEFAULT_SIDE_LENGTH, DEFAULT_SIDE_LENGTH, DEFAULT_SIDE_LENGTH);
}

public Triangle(String color, boolean filled) {
    this(DEFAULT_SIDE_LENGTH, DEFAULT_SIDE_LENGTH, DEFAULT_SIDE_LENGTH, color, filled);
}

public Triangle(double side1, double side2, double side3){
    this(side1, side2, side3, DEFAULT_COLOR, DEFAULT_FILLED);
}

public Triangle(double side1, double side2, double side3, String color, boolean filled) {
    super(/* arguments */);
    //assign sides, color, filled to their fields
}

In essence, you'll require a constructor which takes all of the relevant information (either directly as parameters, or as a data object). From there, it's a matter of properly supplying defaults to the parameters you're missing. In quite a few cases, you'll be able to overload your constructors in a manner that each constructor can specify the defaults, unless you have two disjoint sets of arguments:

public Example() {
    this("default-for-one");
}

public Example(String one) {
    this(one, "default-for-two");
}

public Example(String one, String two) {
    super(); //call the parent constructor if we have one
    //assign fields
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rogue