2 Replies Latest reply: Jun 20, 2007 6:29 PM by 3004 RSS

    Implicit Constructor

      in C++ , if i define a one argument constructor , then i must define a zero argument constructor <which acts as the implicit constructor> for the class

      Does this constraint holds for Java too ?
        • 1. Re: Implicit Constructor
          Why not try it and see?
          • 2. Re: Implicit Constructor
            Constructor rules:

            1) Every class has at least one ctor.

            1.1) If you do not define an explicit constructor for your class, the compiler provides a implicit constructor that takes no args and simply calls super().

            1.2) If you do define one or more explicit constructors, regardless of whether they take args, then the compiler no longer provides the implicit no-arg ctor. In this case, you must explicitly define a
            public MyClass() {...} 
            if you want one.

            1.3) Constructors are not inherited.

            2) The first statement in the body of any ctor is either a call to a superclass ctor
            or a call to another ctor of this class
            2.1) If you do not explicitly put a call to super(...) or this(...) as the first statement in a ctor that you define, then the compiler implicitly inserts a call to super's no-arg ctor
            as the first call. The implicitly called ctor is always super's no-arg ctor, regardless of whether the currently running ctor takes args.

            2.2) There is always exactly one call to either super(...) or this(...) in each constructor, and it is always the first call. You can't put in more than one, and if you put one in, the compiler's implicitly provided one is removed.