SCJP Certification

Sun Certified Java Programmer Certification exam essentials

November 17th, 2009

Constructors

Java Questions, by Daisy Williams.

If a class Student creates two student objects. Which of the following statements are true about its constructors?

A: The compiler can call to super() in any constructor that has a call to this().
B: If a constructor is not declared in the code, a default constructor will be automatically generated by the compiler.
C: Constructors can be overridden.
D: The compiler cannot call to super() in any constructor that has a call to this()

ANSWERS:

If a constructor has a call to this(), the compiler will know that the constructor is not using the call to super(). A constructor can never have both a call to super() and a call to this() because one of these will be used as the first statement in the constructor. The compiler will not put a call to super() that is using a call to this().

If a constructor is not declared in the code, a default constructor will be automatically generated by the compiler. A constructor without any parameters is known as a default constructor. When a class is defined without any constructor, the Java compiler provides an implicit default constructor.

When the implicit default constructor of a class is invoked, it implicitly calls the no parameter constructor in the superclass. This action taken by a default constructor ensures that the inherited state of the object is initialized properly. In addition, it initializes all the instance variables in the object to the default value depending on their data type.

The default constructor of a class has the same access modifier with which the class has been declared. For example, the default constructor of a public class is implicitly given the public access. The default constructor of a protected class is implicitly given the protected access. The default constructor of a private class is implicitly given the private access. Otherwise, the default constructor has the default access implied by no access modifier.

Share

Back Top

Responses to “Constructors”

Comments (0) Trackbacks (0) Leave a comment Trackback url
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*