Default constructors and inheritance in Java

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

Typically, if you write a class and do not include any constructor, Java automatically provides you with a default constructor (one without parameters) that initializes all class instance variables (if any) with some default values ​​(0, null or false ) However, if you write a constructor with some parameters and do not create a default constructor, then Java does not provide a default constructor. My question is: what happens to classes that inherit from other classes - if I write a constructor with some parameters in them, but don’t include the default constructor, do they inherit the default constructor of the superclass?

+50
java inheritance initialization default-constructor
Feb 08 '09 at 11:10
source share
11 answers

Always.

+61
Feb 09 '09 at 3:38
source share

Constructors are not inherited.

In addition, field initialization is performed by the virtual machine, and not by default constructor. The default constructor simply calls the default constructor of the superclass, and the default constructor of Object is empty. A good point of this design is the lack of access to uninitialized fields.

+52
Feb 08 '09 at 11:17
source share

If you are not using super (...), the constructor calls the empty constructor of its parent. Note. This is done on all your classes, even those that extend Object.

This does not inherit; subclasses do not receive the same constructors with the same arguments. However, you can add constructors that call one of the superclass constructors.

+11
Feb 08 '09 at 11:18
source share

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

So, when you have a superclass with only a parameterized constructor and a default constructor, and the base class does not have an explicit call to the parameterized constructor of the superclass, the JVM provides super (); a call that causes an error because there is no default constructor for the superclass, therefore we either provide the default constructor in the superclass, or we explicitly call the constructor with parameterization of the superclass in the constructor of the base class. when we give an explicit call, then the JVM does not bother with the string super (); since the constructor call must be the first expression of a method that cannot happen (due to our explicit call).

+6
Nov 29 '12 at 18:49
source share

Section 8.8.9 of the Java Language Specification explains in detail what happens:

If the class does not contain constructor declarations, then the default constructor is implicitly declared. The default constructor form 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 the non-implicit internal member class, where the default constructor implicitly declares one formal Parameter representing the directly incoming instance of the class (Β§8.8.1, Β§15.9.2, Β§15.9.3).
  • The default constructor does not have throw suggestions.
  • If the declared class is the desired class object, then the default constructor has an empty body. Otherwise, the default constructor simply calls the superclass constructor with no arguments.

You can see that there is no inheritance: all that is is β€œcompiler magic” with an implicit default constructor declaration. The specification also makes it clear that the default constructor is added only when the class has no constructors at all, which means that the answer to your question is β€œno”: after giving the class access to the constructor by default, its superclass is lost.

+5
Feb 13 '15 at 15:37
source share

If you provide a constructor, then Java will not generate an empty default constructor. This way, your derived class can only call your constructor.

The default constructor does not initialize your personal variables to default values. Proof that you can write a class that does not have a default constructor and has its private members initialized with default values. Here is 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); } } 
+3
Feb 08 '09 at 11:25
source share

The Thumb rule is that Sub Class should call any constructor from the base class. therefore, if you do not have a default value, call an existing one from a subclass. other wise ones implement an empty const in the base class to avoid compilation problems

+2
Jun 23 '11 at 11:22
source share

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

+2
Mar 23 '17 at 9:31 on
source share

When we do not create a constructor, Java automatically creates a default constructor. But when we create one or more custom constructors with arguments, Java does not create default constructors. If we create one or more constructors and want to create an object without any constructor arguments, we must declare an empty constructor.

+1
Jan 26 2018-11-11T00:
source share

There will be a compile-time error ... because the compiler is looking for Constructor he Superclass by default, and if it is not ... its error ... and the program will not compile ...

0
Aug 30 '17 at 18:37
source share

Any constructor in a subclass will invoke the constructor without arguments (or the default constructor) of the parent class. if you define a parameterized constructor in the parent class, then you have to explicitly call the constructor of the parent class using the super keyword, otherwise this will cause a compilation error.

 class Alpha { Alpha(int s, int p) { System.out.println("base"); } } public class SubAlpha extends Alpha { SubAlpha() { System.out.println("derived"); } public static void main(String[] args) { new SubAlpha(); } } 

The above code will throw a compilation error:

 prog.java:13: error: constructor Alpha in class Alpha cannot be applied to given types; { ^ required: int,int found: no arguments reason: actual and formal argument lists differ in length 1 error 

The above error occurred because neither we have any argument constructor / default constructor in the parent class, nor do we call the parameterized constructor from the subclass.

Now, to solve this, either call a parameterized constructor, for example like this:

 class Alpha { Alpha(int s, int p) { System.out.println("base"); } } public class SubAlpha extends Alpha { SubAlpha() { super(4, 5); // calling the parameterized constructor of parent class System.out.println("derived"); } public static void main(String[] args) { new SubAlpha(); } } 

Exit

 base derived 

or

define a constructor with no arguments in the parent class as follows:

 class Alpha { Alpha(){ } Alpha(int s, int p) { System.out.println("base"); } } public class SubAlpha extends Alpha { SubAlpha() { System.out.println("derived"); } public static void main(String[] args) { new SubAlpha(); } } 

exit

 derived 
0
May 19 '19 at 7:47
source share



All Articles