Default constructors and inheritance in Java

2019-01-03 16:08发布

I have a question about default constructors and inheritance in Java.

Generally, if you write a class and do not include any constructor, Java provides automatically for you a default constructor (one without parameters), which initializes all instance variables of the class (if there are any) with some default values (0, null, or false). If you write a constructor, however, with some parameters, and you don't write any default constructor, then Java does not provide a default constructor. My question is: what is the case with classes, which inherit from other classes - if I write a constructor with some parameters in them, but don't include a default constructor, do they inherit the default constructor of the super class?

10条回答
Ridiculous、
2楼-- · 2019-01-03 16:28

The basic rule is a call (or invocation) to a constructor should be the first statement that JVM needs to execute,

So when you have a super class with only parameterized constructor and no default constructor, and base class has no explicit call to the parameterized constructor of the super class, JVM provides the super(); call which throws error as there is no default constructor for the super class, so either we provide a default constructor in the super class or we explicitly call the parameterized constructor of the super class in the base class constructor. when we give the explicit call, then JVM doesn't bother to put the line super(); as constructor invocation should be the first statement of the method, which cannot happen (because of our explicit call).

查看更多
倾城 Initia
3楼-- · 2019-01-03 16:29

Thumb Rule is that Sub Class should call any constructor from the base class. so if you don't have the default const the call the existing one from sub class. other wise implement the empty const in the base class to avoid the compilation problem

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2019-01-03 16:32

Section 8.8.9 of the Java Language Specification explains in details what is going on:

If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:

  • The default constructor has the same accessibility as the class (§6.6).
  • The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).
  • The default constructor has no throws clauses.
  • If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

You can see that there is no inheritance going on here: all there is to it is the "compiler magic" with implicitly declared default constructor. The specification also makes it clear that the default constructor is added only when the class has no constructors at all, meaning that the answer to your question is "no": once you give a class a constructor, the access to the default constructor of its superclass is lost.

查看更多
你好瞎i
5楼-- · 2019-01-03 16:32

The answer to your question is very simple. Implicitly(Invisible), the first statement in any constructor is 'super();' i.e. the call to super class's no parameter constructor, until you change it explicitly to something like 'this();','this(int)','this(String)','super(int)','super(String)' etc. 'this();' is current class's constructor.

查看更多
走好不送
6楼-- · 2019-01-03 16:35

If you provide a constructor then Java will not generate you a default empty constructor. So your derived class will only be able to call your constructor.

The default constructor doesn't initialize your private variables to default values. The proof is that it's possible to write a class that doesn't have a default constructor and has its private members initialized to default values. Here's an example:

public class Test {

    public String s;
    public int i;

    public Test(String s, int i) {
        this.s = s;
        this.i = i;
    }

    public Test(boolean b) {
        // Empty on purpose!
    }

    public String toString() {
        return "Test (s = " + this.s + ", i = " +  this.i + ")";
    }

    public static void main (String [] args) {
        Test test_empty = new Test(true);
        Test test_full = new Test("string", 42);
        System.out.println("Test empty:" + test_empty);
        System.out.println("Test full:"  + test_full);
    }
}
查看更多
你好瞎i
7楼-- · 2019-01-03 16:36

Constructors are not inherited.

Also, the initialization of fields is done by the virtual machine, not the default constructor. The default constructor just invokes the default constructor of the superclass, and the default constructor of Object is empty. The good point of this design is that there is no way to ever access uninitialized fields.

查看更多
登录 后发表回答