• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inheritance question

 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answer C is incorrect or am I missing something?

source: scjptest.com

 
krishna bala
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please tell me the relation between classes, i didnot see any relation ?
 
Henry Wong
author
Marshal
Pie
Posts: 21423
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only one constructor will be invoked - there is no connection between those classes
 
Jason Irwin
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Jason, thanks. Object's constructor will run also
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Himanshu Kansal
Ranch Hand
Posts: 257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was cool. Invisible inheritance and the answer given correct if it is case insensitive "c" ... hehe
 
Abhik Ghosh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it me or is there something missing out there ,I don't understand !
 
avi sinha
Ranch Hand
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
S Ali
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 453
Google Web Toolkit Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ken Truitt
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Ken,
The answer B is correct in that case. For answer C to be correct the code should be like --



Regards
Salil Verma
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic