How to implictly call parent class's constructor

12,604

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);
    }
}
Share:
12,604
Linghua Jin
Author by

Linghua Jin

Updated on June 22, 2022

Comments

  • Linghua Jin
    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
    Ted Hopp almost 11 years
    A 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
    Anirudha almost 11 years
    @TedHopp ohh yes..indeed..its applicable to any constructor..edited the ans..thx
  • 王奕然
    王奕然 almost 11 years
    just 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
    Ted Hopp almost 11 years
    @user2245634 - I'd put init in B. Also, the factory method would be a generic method in B; you don't need a separate class. C and D would not need to override init for the sake of initializing a (although they might want to for other initialization).
  • 王奕然
    王奕然 almost 11 years
    thanks,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
    Ted Hopp almost 11 years
    @user2245634 - The idea is to pass B.class as an actual argument to the factory method. It internally uses classArg.newInstance() rather than the new 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 years
    yes,use classArg.newInstance(),but also need to call init method after that
  • Ted Hopp
    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 be Class<T>. Then, since C declares an init method, the factory method can call it. Because of polymorphism, if the actual class passed as an argument overrides init, the overridden method will be called. I fail to see why a factory method won't work for this.
  • Sunil Chawla
    Sunil Chawla over 9 years
    Well.. we must also note that super(x) must be the very first line in the child class constructor.
  • Tomáš Zato
    Tomáš Zato over 9 years
    That'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.