| Author |
inheritance question
|
Ken Truitt
Ranch Hand
Joined: Aug 23, 2007
Posts: 124
|
|
Answer C is incorrect or am I missing something?
source: scjptest.com
|
SCJP 88% | SCWCD 84%
|
 |
krishna bala
Ranch Hand
Joined: Jul 20, 2009
Posts: 48
|
|
can you please tell me the relation between classes, i didnot see any relation ?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Answer C is incorrect or am I missing something?
Yes. Answer C is incorrect. But you do know that you can also confirm it by compiling and running the code too.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
Only one constructor will be invoked - there is no connection between those classes
|
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
|
 |
Jason Irwin
Ranch Hand
Joined: Jun 09, 2009
Posts: 327
|
|
Lukas Smith wrote:Only one constructor will be invoked - there is no connection between those classes
You mean two constructors, don't you? But only one from that code listing.
|
SCJP6
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
Yeah Jason, thanks. Object's constructor will run also
|
 |
Nitish Bangera
Ranch Hand
Joined: Jul 15, 2009
Posts: 536
|
|
|
I guess the question forgot the extends from Small2 and Small3. well current question will invoke 2 constructors and one of them is the super constructor that is Object and the other is Small3.
|
[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
|
 |
Himanshu Kansal
Ranch Hand
Joined: Jul 05, 2009
Posts: 257
|
|
That was cool. Invisible inheritance and the answer given correct if it is case insensitive "c" ... hehe
|
Experience and talent are independent of age
|
 |
Abhik Ghosh
Ranch Hand
Joined: Feb 28, 2008
Posts: 32
|
|
See. there are basically 3 things that gets called when a constructor is invoked.They are in this order:
1.super()
2.init blocks
3.other super constructors
So obviously here the first call in each constructor is tha call to super(), and so Constructor of C calls that of B, which calls that of A.So we get a b c .
|
 |
S Ali
Ranch Hand
Joined: Aug 23, 2009
Posts: 129
|
|
Is it me or is there something missing out there ,I don't understand !
|
SCJP 6
|
 |
avi sinha
Ranch Hand
Joined: Mar 15, 2009
Posts: 452
|
|
Abhik Ghosh wrote:S
1.super()
2.init blocks
3.other super constructors
i didn't understand.... what does it mean " other super constructors " . as per i suppose there may be only one superclass for a class as a class can't extend more than one class.
in my opinion the sequence will be :
static blocks -- > super constructor --- > init() blocks ---> constructor
the same rule will be followed by the superclass for ex: if c extends b extends a
then
static blocks --> init of a ---> constructor of a ---> init of b ---> constructor of b ---> init of c ---> constructor of c
in the above question if they are related to each other using inheritance "c" would be correct
ie if small3 extends small2 extends small
avi sinha
|
SCJP 5.0 SCWCD 5.0
|
 |
Ankit Garg
Saloon Keeper
Joined: Aug 03, 2008
Posts: 9189
|
|
avi sinha wrote:
static blocks -- > super constructor --- > init() blocks ---> constructor
:
static blocks --> init of a ---> constructor of a ---> init of b ---> constructor of b ---> init of c ---> constructor of c
I think you should keep static blocks out of this. They are executed only the first time a class is loaded and not with each instance. The sequence is superclass constructor, then initialization expressions and instance initializer blocks in the order of their declaration, then the constructor body. This is not very difficult to understand so there should be no confusion.
As far as the original question goes, you can say that the question has a bug, and for C to be the right answer, there must be inheritance relationship between Small, Small2 and Small3...
|
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
|
 |
S Ali
Ranch Hand
Joined: Aug 23, 2009
Posts: 129
|
|
As far as the original question goes, you can say that the question has a bug, and for C to be the right answer, there must be inheritance relationship between Small, Small2 and Small3...
what bug are you talking about? seriously is there something missing!
|
 |
avi sinha
Ranch Hand
Joined: Mar 15, 2009
Posts: 452
|
|
Ankit Garg wrote:
I think you should keep static blocks out of this. They are executed only the first time a class is loaded and not with each instance.
you are correct ankit. i have written static blocks . i haven't specified any particular object of any class. the reason is the same.
avi sinha
|
 |
avi sinha
Ranch Hand
Joined: Mar 15, 2009
Posts: 452
|
|
S Ali wrote:
what bug are you talking about? seriously is there something missing!
the bug is that no any relationship between the classes is specified here that's why the answer can't be "a b c"
avi sinah
|
 |
Fritz Guerilus
Ranch Hand
Joined: Jun 20, 2009
Posts: 65
|
|
Ken,
Are you saying that scjptest.com is showing that C) 'a b c'; is the correct answer? .
If that is the case, then you should email the creators and let them know.
There is nothing wrong with this code, just like everyone on this thread is saying.
I also complied and executed the program and B) not C) is the correct answer
-Fritz
|
SCJP 6.0
|
 |
Ken Truitt
Ranch Hand
Joined: Aug 23, 2007
Posts: 124
|
|
You all realize this thread was more than a month old? Anyway, the test that this comes from at scjptest.com is pretty
buggy. Like many of the tests listed in the FAQ, it has a very good interface but the questions need editing. It
*could* be a great resource i they'd debug it and edit some of the wrong questions.
|
 |
Abhik Ghosh
Ranch Hand
Joined: Feb 28, 2008
Posts: 32
|
|
Most definitely the ans is wrong.. it is B) c .. i missed it, thanks Ankit, i need to be more alert.. the classes are missing inheritance. Now when I say "other super constructors", I mean this:
The sequence of the output is numbered. Hope this make it clearer.
|
 |
Salil Vverma
Ranch Hand
Joined: Sep 06, 2009
Posts: 219
|
|
Hey Ken,
The answer B is correct in that case. For answer C to be correct the code should be like --
Regards
Salil Verma
|
Regards
Salil Verma
|
 |
 |
|
|
subject: inheritance question
|
|
|