Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constractors

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is from Monktest
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?
A. Base() { }
B. Base(int j) { }
C. Base(int j, int k) { }
D. Base(int j, int k, int l) { }

I think the right answer is: A and B. But the real answer was A and C.
If someone has any explanations, please.
I'd appreciate this and it can help others.
Thank you.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai aull,
The right answer for the question is A and C only. Its because of this.
In the Test class, there are two constructors (1) one with no arguments and (2) one with two arguments.
In case (1)
The no argument constructor has no statements in its block. Hence on declaring an object of Test without Arguments, a internal call will be made to super() i.e. the one without any argumnts.
In case (2)
The two argument constructor (of Test class) is calling super(j,k) with two arguments. Hence a two argument constructor to the class Base should exist for the call.
Summing up case (1) and (2), there need to be two constructors one with no arguments and other with two arguments.
Hence A and C are required.
I hope this answer would be helpful
JVRN.
Note. : If you still have any doubts, write to me.
In the second case

Originally posted by Aull Sahar:
This question is from Monktest
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?
A. Base() { }
B. Base(int j) { }
C. Base(int j, int k) { }
D. Base(int j, int k, int l) { }

I think the right answer is: A and B. But the real answer was A and C.
If someone has any explanations, please.
I'd appreciate this and it can help others.
Thank you.



------------------
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that the answer is a & c but, Test Class does not have a no - arg constructor as you say - the default no args constructor is generated only when no constructor is declared explicitly, right? - Doesn't it have something to do with the fact that the no-args constructor of the base class is invoked when the constructor in the sublcalss does not explicitly invoke super(some_args)??
Since the extended class invokes super(j,k), there must be a constructor matching Base(j,k). This means that there will be no default-no-args constructor supplied.

Originally posted by jvijay:
Hai aull,
The right answer for the question is A and C only. Its because of this.
In the Test class, there are two constructors (1) one with no arguments and (2) one with two arguments.
In case (1)
The no argument constructor has no statements in its block. Hence on declaring an object of Test without Arguments, a internal call will be made to super() i.e. the one without any argumnts.
In case (2)
The two argument constructor (of Test class) is calling super(j,k) with two arguments. Hence a two argument constructor to the class Base should exist for the call.
Summing up case (1) and (2), there need to be two constructors one with no arguments and other with two arguments.
Hence A and C are required.
I hope this answer would be helpful
JVRN.
Note. : If you still have any doubts, write to me.
In the second case


[This message has been edited by firstname lastname (edited September 14, 2000).]
[This message has been edited by firstname lastname (edited September 14, 2000).]
 
Vim Win
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Regarding to super call : Call to the Base class constructor using super() should be the first statement in the Derived class constructor. No other statements can precede the call to super().
Yes, Default constructor is created only when there are no other explicit constructors declared for the class.
In the Derived class constructor, if no call to the base class constructor by using super() exists, by default the call is made to the no argument constructor of the Base class.
If a call to the super() exists, then the constructor of the Base class with the appropriate arguments in the super() call is invoked.
If there is only a call to super() with arguments, there is no need for a no-argument constructor (note that no other derived class should call a no-argument constructor).
I hope this would have answered your question.
JVRN.

Originally posted by firstname lastname:
I agree that the answer is a & c but, Test Class does not have a no - arg constructor as you say - the default no args constructor is generated only when no constructor is declared explicitly, right? - Doesn't it have something to do with the fact that the no-args constructor of the base class is invoked when the constructor in the sublcalss does not explicitly invoke super(some_args)??
Since the extended class invokes super(j,k), there must be a constructor matching Base(j,k). This means that there will be no default-no-args constructor supplied.

[This message has been edited by firstname lastname (edited September 14, 2000).]
[This message has been edited by firstname lastname (edited September 14, 2000).]



------------------
 
Aull Sahar
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much for help.
This is very nice when there are kind people who can help you to solve some problems.
Thank you again to everyone who replied for my question.
 
I don't even know how to spell CIA. But this tiny ad does:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic