The default constructor Assignment Help

Assignment Help: >> Constructors in Java >> The default constructor

The default constructor:

You may be wondering how we have managed to construct objects thus far, given that we have not been writing constructors. The answer is that if you do not write a constructor for your class, Java kindly provides you with one. This special constructor is called the default constructor and it has no arguments. You will not see this constructor appear in your class, but you may assume that it is present, if you do not write any constructors of your own. 

In order to understand this better, we first need to explain the use of the keywords this and super in the context of constructors. 

The keyword this

The word this can be used to invoke a constructor. You can think of this as a reference to the class in which it appears. 

For example, if the Java interpreter encounters the statement: 

this(1000, 300);

as the ?rst statement in a constructor ClassName(), it will look for a matching ClassName constructor with two arguments. When that ClassName constructor is invoked, the three-step construction procedure is followed as before. For example, consider the code shown below, which contains the skeletal description of ThreeInt. The class has three instance variables p, q and r and four constructors. The variables are not accessible to other classes, as we have declared them to be private. 

public class ThreeInt 

{

private int p, q, r;

public ThreeInt ()

{

this(0, 0, 0);

}

public ThreeInt (int a)

{

this(a, 0, 0);

}

public ThreeInt (int a, int b)

{

this(a, b, 0);

}

public ThreeInt (int a, int b, int c)

{

p=a;q=b;r=c;

}

// methods associated with ThreeInt

}

The first three constructors make use of the three-argument constructor. This is a way of reusing constructors you have written and can assist with readability when you have several constructors. Do not think of this(a, b, 0) as a call to a method. It is only possible to invoke constructors in this way in this context: from another constructor, and then you must do so at the beginning of the constructor. (Other code may appear after the constructor invocation.) This is another significant difference between constructors and methods. In addition, a constructor cannot invoke itself (whereas a method can). This helps to ensure that objects are correctly initialized. For example, the following 'cyclic' code is illegal: 

public ThreeInt ()

{

this();

}

The keyword super

Assume that we want to extend the class ThreeInt presented earlier, by adding a further integer instance variable and a few other methods. 

public class FourInt extends ThreeInt
{
private int s;
// methods

}

Now we would want to ensure correct initialization of the instance variables p, q and r defined in ThreeInt, but we would have to rely on ThreeInt to do this, as the data is private. Some code for a constructor in FourInt that would do this is shown below:
public FourInt (int a, int b, int c, int d)
{
super(a, b, c);
s=d;

}

The code super(a, b, c) results in the three-argument constructor of ThreeInt being executed, which results in the values of a, b and c being assigned to the instance variables p, q and r associated with this class. After this, the instance variable s associated with FourInt is assigned the value d. As usual, this constructor code is executed after the 'empty' object is created and any explicit initialization is performed. The invocation of the superclass constructor can be done only as the first statement of the enclosing constructor, to ensure that all superclass data is correctly initialized, following the three-step procedure outlined earlier. 

 

Java Assignment Help - Java Homework Help

Struggling with java programming language? Are you not finding solution for your The default constructor homework and assignments? Live The default constructor experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The default constructor homework help, java assignment help and The default constructor projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.

Why Expertsmind for assignment help

  1. Higher degree holder and experienced experts network
  2. Punctuality and responsibility of work
  3. Quality solution with 100% plagiarism free answers
  4. Time on Delivery
  5. Privacy of information and details
  6. Excellence in solving java programming language queries in excels and word format.
  7. Best tutoring assistance 24x7 hours

 

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd