Going to the superclass

I was wondering if I have an abstract superclass with x different constructors, and I want to be able to use all these constructors in a subclass, I need to write all x-constructors in a subclass and just let them all call super (...)? Looks like redundant code.

Clarity example:

public class SuperClass {
public SuperClass() {
...
}
public SuperClass(double d) {
...
}
public SuperClass(BigDecimal b) {
...
}
public SuperClass(BigDecimal b, String s) {
...
}
[...]
}

Do I need:

public class SuperClass {
public SubClass() {
super();
}
public SubClass(double d) {
super(d);
}
public SubClass(BigDecimal b) {
super(b);
}
public SuperClass(BigDecimal b, String s) {
super(b, s);
}
[...]
}
+5
source share
4 answers

But you have to do it this way. This way you decide what you want to reveal in the subclass. Also, some constructors may not be suitable for a subclass class, so this does not work by default unless you code it explicitly.

+5
source

. . ( ), . , .

: super(), . , .

, , , , .

+2

:

public class SuperClass {
public SuperClass() {
...
}
public SuperClass(double d) {
...
}
public SuperClass(BigDecimal b) {

}
public SuperClass(BigDecimal b, String s) {
this(b);
}
[...]
}

:

public class SubClass extends SuperClass{
public SubClass() {
super();
}
public SubClass(double d) {
super(d);
}

public SuperClass(BigDecimal b, String s) {
super(b, s);
}
[...]
}
+1

If you need each type of constructor in both parenr and child, then yes, you need to explicitly specify them, but you do not need to explicitly call it super().

+1
source

All Articles