当前位置: 动力学知识库 > 问答 > 编程问答 >

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: