• Post Reply Bookmark Topic Watch Topic
  • New Topic

Order Of Constructor calling  RSS feed

 
Rajat Sharmanaive
Ranch Hand
Posts: 41
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

This doubt is w.r.t constructor chaining. Please look at the snippet. For this entire session please assume (and i am risking wrath here) that, Default constructor = No-args constructor. Just pretend yourself to be a JVM and have no idea if the constructor is a default or an explicit no-args.




Now from what i know, in any subclass constructor, compiler inserts a call to super() only if we DO NOT ourselves give a parameterized call to super (but i have given here so it should not call and therefore should not care if it's there or not). In the above code however, as soon as i comment B's default constructor, there is an error. Even though i do not instantiate C in a way that, according to me should require any default constructor (C's or B's).



in my mind the calling hierarchy should be: C(int i) --> super(i) --> C(int i). So for me B's default constructor does not figure here. But if i comment it AND DO NOT COMMENT C's default (which i think does not matter here) JVM complains "cannot find symbol COnstructor B()".
IMPORTANT : when i comment C's default, the problem goes away.

Is there any rule like:
If any subclass has a default constructor, superclass must provide one. Even if you never instantiate the subclass in a way that requires a default constructor. Please guide. Constructors really are whimsical.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you comment out B's no-args constructor but not C's no-args constructor, it will not compile, because C's no-args constructor still needs to call B's no-args constructor. It doesn't matter if you ever use C's no-args constructor to instantiate an object or not.

Note that the Java compiler will add a no-args constructor to a class automatically, but it only does that if the class does not have any constructors at all. Since your class B already has a constructor defined (the one that takes an int), no no-args constructor will be added automatically.

The rules are:

  • When you create an instance of a class that extends another class, then first the superclass part of the object is created. A constructor of the superclass will be called as part of this.
  • If a constructor does not have an explicit super call, then the compiler will automatically add super();
  • If a class does not have any constructors defined, then the compiler will automatically add a no-args constructor.

  •  
    Rajat Sharmanaive
    Ranch Hand
    Posts: 41
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Jesper. That was really helpful.
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    There is bound to be something in the Java™ Language specification (JLS): try this, and if it doesn't help, go through the JLS index. Warning: the JLS is by no means easy to read.
     
    Rajat Sharmanaive
    Ranch Hand
    Posts: 41
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell
    Hi.

    Yes jls is tough to decipher. Tougher still if you last studied context free grammar and other machine notations in college, about 2 years back. Anyways, i did find Jesper's reply quite helpful and based on many hit and trials i have performed, i have (sort of) concluded the following:

    1) If a no-args constructor is required (for whatever reason - which is more important to identify) JVM will oblige if and only if there is no constructor explicitly provided from our side. Even if we supply a single parametrized constructor, JVM gets upset and vows not to provide any default (his version of a no-args) constructor.

    2) The more important point is to identify if a no-args constructor is required. In following situations it will be:
    --> Direct instantiation of the instance i.e no inheritance involved. . This is obvious.
    --> First call in any constructor (and this is on per constructor basis) is a super(). This is provided by JVM and hence makes the condition 1 of a no-arg constructor being required true for the super class. A default call to super() will not be there if we replace it with a parametrized super() or a parametrized this().
    --> so if in any constructor there is a call to this() or super(obviously parametrized in this case, default is bound to make condition 1 true), and there is no no-arg constructor in the super class (but there is a parametrized 1), that particular constructor is safe as far as those "cannot find symbol" errors are concerned.
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16060
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Correct, that is a more detailed description of the rules than I wrote.
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rajat Sharmanaive wrote: . . . JVM gets upset and vows not to provide any default . . . constructor. . . .
    "JVM gets upset"??

    It's more that when the compiler (not the JVM) goes through the class and doesn't find a constructor, it thinks, "There's no constructor; how can the JVM instantiate this class? I shall have to provide a default constructor." If you have written a constructor, with or without parameters, the compiler is programmed to think this is how you want the class instantiated, so there is no need for a default constructor.

    And I am pleased to see you have been saying "default constructor" and "no-arguments constructor" correctly in your more recent posts
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If I remember correctly, the "javac" tool will provide a default constructor if and only if there is no other constructor written.
     
    Rajat Sharmanaive
    Ranch Hand
    Posts: 41
    Eclipse IDE Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Campbell Ritchie wrote: If you have written a constructor, with or without parameters, the compiler is programmed to think this is how you want the class instantiated, so there is no need for a default constructor.


    Hi Campbell

    This is indeed the explanation. Thanks.
     
    Campbell Ritchie
    Marshal
    Posts: 56541
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!