简体   繁体   中英

Why does the subclass in this Java code have a default constructor if the super-class does not have one?

I have looked around for the answer to this question, but so far I haven't understood this. I have 3 Java classes, BinaryTree and HuffmanTree (which extends BinaryTree) and HuffmanData (used in HuffmanTree, irrelevant to this question)

HuffmanTree explicitly calls super() in its constructors. What I am unable to understand is why does it include a call to the default constructor? The superclass BinaryTree does not have a default constructor at all, so according to my understanding the code should throw an Exception, but it doesn't.

Here is the code for BinaryTree :

public class BinaryTree {
    private Comparable data;
    private BinaryTree left;
    private BinaryTree right;
    public final static BinaryTree NIL = new BinaryTree(null, null, null);

    // Constructor 1
    public BinaryTree (Comparable data, BinaryTree left, BinaryTree right) {
        this.data = data;
        this.left = left;
        this.right = right;
    }

    // Constructor 2
    public BinaryTree (Comparable data) {
        this(data, BinaryTree.NIL, BinaryTree.NIL);
    }
    .
    .
    . // other methods
}

And here is the code for HuffmanTree :

public final class HuffmanTree extends BinaryTree implements Comparable {
    private String[] table;
    private static final int NUM_CHARS = 256;
    private static final HuffmanTree NIL = new HuffmanTree();
              // Calls default constructor in this class, but how does it work?

    // Constructor 1 --- what does this do??
    private HuffmanTree() {}

    // Constructor 2
    private HuffmanTree (char c, int f) {
        super(new HuffmanData(c,f), NIL, NIL);
    }

    // Constructor 3 
    private HuffmanTree (HuffmanTree left, HuffmanTree right) {
        super(new HuffmanData( (char)0, left.frequency()+right.frequency()),
                                                 left, right);
    }
    .
    .
    . // other methods
}

Apologies if the question is unclear. Please let me know how I can explain it better.

This my friend would produce a compiler error no suitable constructor found for BinaryTree() .

A default no-arg constructor would be created for a Class only if you don't provide any constructor for the class. If you provide a constructor to the class, then you have to create a no-arg constructor on your own.

JLS states that

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause.

The private no-args constructor does nothing useful except cause a compilation error. Looks like somebody's attempt to make it unusable by making it private, but the presence of the other constructor already prevents the compiler from generating a default constructor.

I don't know why you think an exception will be thrown, unless you're confusing exceptions and compile errors. Compile errors are not exceptions, and they are printed, not thrown.

In Java, default constructor is automatically created by JVM whether you write it or not. So in your code for class of BinaryTree default constructor is automatically created and when object of HuffmanTree calls default constructor it calls default constructor of BinaryTree.

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