I was wondering why in java constructors are not inherited? You know when you have a class like this:
public class Super {
public Super(ServiceA serviceA, ServiceB serviceB, ServiceC serviceC){
this.serviceA = serviceA;
//etc
}
}
Later when you inherit from Super
, java will complain that there is no default constructor defined. The solution is obviously something like:
public class Son extends Super{
public Son(ServiceA serviceA, ServiceB serviceB, ServiceC serviceC){
super(serviceA,serviceB,serviceC);
}
}
This code is repetitive, not DRY and useless (IMHO)... so that brings the question again:
Why java doesn't support constructor inheritance? Is there any benefit in not allowing this inheritance?
Son
is repetitive. It is for this reason that C++ now allows derived classes to inherit base constructors (see www2.research.att.com/~bs/C++0xFAQ.html#inheriting). Note that I emphasize "allow" because the derived class must explicitly declare that it uses the constructors in the base.private final int foo;
in the superclassSuper
, you can not assign a value to foo in the inherited Constructor inSon
because its private inSuper
.