• 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

Question on constructors

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a doubt regarding the following question from RHE(chapter6: Objects and Classes Test urself Question no.7).
The question is
public class Test extends Base {
public Test(int j) {
}
public Test(int j, int k) {
super(j,k);
}
}

Which of the following forms of constructor must exist emplicitely in the definition of base class?
ans
a.Base(){}
b.Base(int j){}
c.Base(int j,int k){}
d.Base(int j,int k,int l){}
The answer given is a and c. How come? I answered it as only c
Can anybody explain that to me.
Thanks in advance
Renuka
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is to do with "when is the default constructor provided by default? " In the derived class i.e. "Test" there is a constructor that gives an explicit call (super(j.k)) to the Base class constructor. So, your answer "c" is obviously correct. But(there is always a but, huh! ;-)), now in the base class the constructor Base() will not be provided by default. You will need the default constructor in the Base class because of the Test(int j) constructor. When this "Test(int j)" constructor is executed it will also invoke the default Base() constructor. I hope this has not been confusing!!
Ankur
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Ankur has explained things nicely IMO.
Rahul
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Base(){} will never be executed though.
- rexian
 
rexian
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An addendum to the previous post:
public class Test extends Base {
public Test(int j, int k) {
super(j,k);
System.out.println("Test(j,k)");
}
public Test(int j) {
System.out.println("Test(j)");
}
public static void main(String[] args){
System.out.println("initiating Test(1)" );
new Test(1);
System.out.println("initiating Test(1,10)" );
new Test(1,10);
}
}
class Base {
public Base(){
System.out.println("Base()");
}
public Base(int j,int k){
System.out.println("Base(j,k)");
}
}

Outputs:
initiating Test(1)
Base()
Test(j)
initiating Test(1,10)
Base(j,k)
Test(j,k)
Explanation:
Self-explanatory
- rexian
 
Renuka Kilambi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank a lot guys for ur explainations!!
Renuka
 
For my next trick, I'll need the help of a tiny ad ...
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic