How to implictly call parent class's constructor
Solution 1
Constructors are not inherited. You will need to declare a constructor in C
that takes an argument. It will need to invoke the appropriate superclass constructor (if you don't want the default superclass constructor to be called).
The only constructor you can avoid declaring is the default, no-arg constructor, and that only if you declare no constructors. That's because the compiler will generate one for you if you have no constructors declared. The compiler always inserts a call to the default superclass constructor unless you explicitly call a superclass constructor.
Note also that if you do not explicitly call a superclass constructor and there is no default constructor in the superclass, you will get a compile-time error.
EDIT: If you have "lots of classes like class C", then an alternative to writing a lot of constructors is to use a factory method pattern. You can have all the classes implement a default constructor and provide a separate init()
method that takes a standard set of arguments. The factory method would accept a Class<? extends C>
object and some initialization arguments, generate a new instance (using the default constructor), and call its init
method with the initialization arguments. That way you only need to override the init
method for those subclasses that need special handling (always remembering to call through to super.init
).
Solution 2
- A constructor implicitly calls the parameter-less constructor of it's immediate super class(only if there's no explicit call)
- When you define your own constructor,the default constructor would not be created.
So,in your case Class C
has a default constructor which would try to implicitly call the default constructor of Class P
which doesn't exits and would fail.
So,you have to do it this way
class P
{
public P(int a)
{
// construct
}
}
class C extends P
{
public C(int x)
{
super(x);
}
}
![Linghua Jin](https://i.stack.imgur.com/girF2.png?s=256&g=1)
Linghua Jin
Updated on June 22, 2022Comments
-
Linghua Jin about 2 years
I want to do following thing:
class P { P(int a) { // construct } } class C extends P { } // in main int a = 2; C foo = new C(a); // can I do this?
I want create child object C by calling parent class P's constructor without writing any constructor in class C like "super(a)". Is that possible?
The idea is that I have a lot of class like "class C" which needs the same constructor functionality as "class P". So I don't want write a constructor method each time I create a new similar class.
Thanks
-
Ted Hopp almost 11 yearsA default constructor implicitly calls the parameter-less constructor of it's immediate super class only if the default constructor does not explicitly call a superclass constructor. However, that's true of any constructor, default or not.
-
Anirudha almost 11 years@TedHopp ohh yes..indeed..its applicable to any constructor..edited the ans..thx
-
王奕然 almost 11 yearsjust like:public class B{protected int a;} class C extends B{proctected void init(int a){this.a =a;}} class Factory{ public static C getC(Class<? extend C> c,int a){C cInstance = c.newInstance(); cInstance.init(a);return cInstance;} class D extends C{@override procted void init(int a){super.init(a);...}}?
-
Ted Hopp almost 11 years@user2245634 - I'd put
init
inB
. Also, the factory method would be a generic method inB
; you don't need a separate class.C
andD
would not need to overrideinit
for the sake of initializinga
(although they might want to for other initialization). -
王奕然 almost 11 yearsthanks,but if factory method pattern must use factory class to produce instance,or call new B() and so on will omit init method
-
Ted Hopp almost 11 years@user2245634 - The idea is to pass
B.class
as an actual argument to the factory method. It internally usesclassArg.newInstance()
rather than thenew
operator to create an instance of whatever class was passed. Explicitly coding the class to be instantiated defeats the present purpose of using any kind of factory pattern. -
王奕然 almost 11 yearsyes,use classArg.newInstance(),but also need to call init method after that
-
Ted Hopp almost 11 years@user2245634 - Yes, that's the idea. The type parameter to the generic factory method would be
<T extends C>
, and the class argument would beClass<T>
. Then, sinceC
declares aninit
method, the factory method can call it. Because of polymorphism, if the actual class passed as an argument overridesinit
, the overridden method will be called. I fail to see why a factory method won't work for this. -
Sunil Chawla over 9 yearsWell.. we must also note that super(x) must be the very first line in the child class constructor.
-
Tomáš Zato over 9 yearsThat's why java is so annoying and slow to write. I now have to implement 7 constructor overloads just because this language totally lacks convenient features.