GeeCON Prague 2014*
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


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "constructor" Watch "constructor" New topic
Author

constructor

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477



Select all valid answers:
A1 The code will fail to compile.
A2 The constructor in A that takes an int as argument will never be called as a result of constructing an object of class B or Test11.
A3 class Test11 has three constructors.
A4 Atmost one of the constructors of each class is called as a result of constructing an object of class Test11.

The given answers are A2 and A3.I feel like A4 also a correct option.Please clarify.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Shiva,

Please quote your sources.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Originally posted by Shiva Mohan:



Select all valid answers:
A1 The code will fail to compile.
A2 The constructor in A that takes an int as argument will never be called as a result of constructing an object of class B or Test11.
A3 class Test11 has three constructors.
A4 Atmost one of the constructors of each class is called as a result of constructing an object of class Test11.

The given answers are A2 and A3.I feel like A4 also a correct option.Please clarify.


If you create an instance of Test11 like this

,

then both the constructor of Test11 that accepts a String and the no-argument constructor of Test11 are called.
Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 477
Thanks for the reply keith.

Any of the constructors of Test11 access atmost one of the constructor of each class since Test11 is a subclass of B,A.Right?
Then why the option A4 is not selected.

from your previous post,what are you saying? Maybe i am stupid to get that.Please help again.

Henry, i forgot.sorry.the question is from javabeat.
[ March 24, 2007: Message edited by: Shiva Mohan ]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Option 4 says that at most one constructor of each class is called if you create an instance of Test11.

This is not true because if you create a Test11 object with the constructor that accepts a String, then that constructor immediately calls the no-argument constructor in Test11, which then calls the no-argument constructor of B, which calls the no-argument constructor of A, which calls the no-argument constructor of Object.
Joan Horta Tosas
Ranch Hand

Joined: Feb 01, 2007
Posts: 59
Doesn't the A1 be valid too? I thought a constructor couldn't return a type... as B does. Maybe I was all this time wrong...
Joan Horta Tosas
Ranch Hand

Joined: Feb 01, 2007
Posts: 59
Found the answer by myself doing tests... It will compile because method boolean B(String msg) is a method, not a constructor... I always thought that class names could just be used for constructors, not other methods, but maybe this thought was teached to me due to legibility.
Well, something new I've learned today!
kishore thota
Greenhorn

Joined: Mar 26, 2007
Posts: 1
Hi,
whenever we create the object reference based on integer constructor it will not call remaining constructor of other classes
 
GeeCON Prague 2014
 
subject: constructor