Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

constructor

 
Shiva Mohan
Ranch Hand
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shiva,

Please quote your sources.

Henry
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 484
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
whenever we create the object reference based on integer constructor it will not call remaining constructor of other classes
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic