简体   繁体   中英

private constructor usage in class

If there is a private constructor, does the JVM insert a call to the super constructor?

I'm referring to the super() call in that private constructor.

class Alpha {
    static String s="";
    protected Alpha(){
        s+="alpha";
    }
}

class SubAlpha extends Alpha{
    private SubAlpha(){
        s+="sub";
    }
}

class SubSubAlpha extends Alpha{
    private SubSubAlpha(){
        s+="subsubAlpha";
    }

    public static void main(String[] args){
        new SubSubAlpha();
        System.out.print(s);   
    }
}

Here I don't get any compilation error. Here in the SubSubAlpha class there is private constructor. Is that compiler insert super() call in that if so, what happens in the SubAlpha class. Even there is private constructor. And if that is not accessed how the inheritance tree continues till the top.

If there is private constructor does the JVM inserts call to super constructor?

Yes

The super constructor will always be called. (You can't instantiate an class, without also instantiating the super class at the same time.)

If you don't do it explicitly yourself, there will be an implicit call inserted for you, no matter if the constructor is private or public.


To be picky: It's actually not the JVM that inserts it, but the Java compiler:

public class Test {
    private Test() {
    }
}

is compiled into

private Test();
  Code:
   Stack=1, Locals=1, Args_size=1
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

Yes. The private constructor has an implicit super() call if there is not explicit super or this constructor call. In this respect, it is no different from other constructors. Naturally, for this to compile, the superclass has to have a no-args constructor that is visible from the class.

However, if a constructor is private , it cannot be called from a subclass ... or from any other code apart from the class itself.

The reason you're not getting a compile-time error is that your class hierarchy isn't what you think it is. I think you meant this:

class SubSubAlpha extends Alpha {

to be this:

class SubSubAlpha extends SubAlpha {

... at which point you will indeed get a compile-time error.

(In other words, yes, there is always a call to the super-constructor.)

This is a nice example from the Java Programmer's SourceBook . Since the constructors print when called, you will see immediately the sequence of the calls. It is from Thinking in Java.

//: Cartoon.java
// Constructor calls during inheritance

class Art {
  Art() {
    System.out.println("Art constructor");
  }
}

class Drawing extends Art {
  Drawing() {
    System.out.println("Drawing constructor");
  }
}

public class Cartoon extends Drawing {
  Cartoon() {
    System.out.println("Cartoon constructor");
  }
  public static void main(String[] args) {
    Cartoon x = new Cartoon();
  }
} ///:~ 

The output for this program shows the automatic calls:

Art constructor
Drawing constructor
Cartoon constructor

Ya, u can use super() to use the function and attribute from the superclass. its the same with normal java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM