my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Constructor" Watch "Constructor" New topic
Author

Constructor

Vidya Singh
Greenhorn

Joined: Mar 21, 2007
Posts: 28
Ques 3 :
Consider the following class definition:
1. public class Test extends Base {
2. public Test(int j) {
3. }
4. public Test(int j, int k) {
5. super(j, k);
6. }
7. }

Which of the following forms of constructor must exist explicitly in the definition of the Base class?


(1) Base() { }
(2) Base(int j) { }
(3) Base(int j, int k) { }
(4) Base(int j, int k, int l) { }


Answer : 1,3
Explanation :
1 and 3 are correct. In the constructor at lines 2 and 3, there is no explicit call to either this() or super(), which means that the compiler will generate a call to the zero argument superclass constructor, as in 1. The explicit call to super() at line 5 requires that the Base class must have a 7.constructor as in 3. This has two consequences. First, 3 must be one of the required constructors and therefore one of the answers.Second, the Base class must have at least that constructor defined explicitly, so the default constructor is not generated, but must be added explicitly. Therefore the constructor of 1 is also required and must be a correct answer.At no point in the Test class is there a call to either a superclass constructor with one or three arguments, so 2 and 4 need not explicitly exist.
-----------

I do not understand why constructor Base() { } is necessary, and default constructor is mentioned explicitly
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
I do not understand why constructor Base() { } is necessary, and default constructor is mentioned explicitly


3rd is necessary and you know it.
But if you will have 3rd in your base class no default constructor would be generated in the base class.
And you can't say something like this

because the first statement inserted would be a call to super()
like this


that is why 1st is necessary too


SCJP || SCWCD

Vidya Singh
Greenhorn

Joined: Mar 21, 2007
Posts: 28
Thanks for explaining. So, that means if you explicitly defines any constructor, then default constructor does not come into picture, and one has explicitly define a default constructor.
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
Originally posted by Vidya Singh:
Thanks for explaining. So, that means if you explicitly defines any constructor, then default constructor does not come into picture, and one has explicitly define a default constructor.


You have to write default constructor only if you are going to use default constructor. (either explicitly or implicitly)


SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
Yes if you define even a sigle constructor in the class.No default constructor would be generated in that class.That is always no-arg. It is well explained in K&B book
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Vidya, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor
 
Similar Threads
Overloading Constructors
Constructor Problem
another question
Constractors
Mock Exam question