• 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

default constructor

 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True or False:

1. The default constructor invokes the no-parameter constructor of the superclass
2. The default constructor initializes the instance variables declared in the class

Ans given : 1 - False and 2 - True

1. I think it is true.
Explanation: When object of subclass is created, first constructor of subclass is checked, for if there is any super keyword exists. If there is any super keyword, then it is executed otherwise default constructor of the superclass is called.

2. - This is false.
Explanation: Instance variables gets initialized even if we create constructor.

Expecting your openion on above two points please.
Thank you
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitin,
I agree with your thinking on first point. Here is the code I wrote just to verify this


class Base {
Base() {
System.out.println("Base ctor");
}
}

class Subclass extends Base {
private int salary;
public static void main(String args[]) {
Subclass s = new Subclass();
System.out.println("The salary is:"+ s.salary);
}
}



In the above code, Subclass class does not have a constructor, hence the default is provided by the compiler. I believe the default constructor provided by the system does have a call to super() and that is why we get the output
Base ctor

Last thing about initializing the member variables, as we can see the output of the salary is 0 ( zero ). This tells us that the default ctor provided for the subclass did initialize the int variable to 0.

I guess both the answers should be true.

Anybody with different opinion.

[ November 15, 2004: Message edited by: Jay Pawar ]
[ November 15, 2004: Message edited by: Jay Pawar ]
 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
U do know that the no parameter constructor is not same as that of default constructor. The default is created when no contructor is declared.

For the second option it's correct cause a constrcutor is supposed to initialise all class variables to their default values and since there is a constructor created by default when no other is created, that wuld also initialise it. I hope i helped a little. Just buzz back if i omitted something.

Saheed.
SCJP 1.4
SCJD(preparing intensively..)

Imagination is better than knowledge..
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following sample code can help us to understand points. Please let me know if anyone have different suggestions.



Thank you
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nitin, you have given a very nice example.

If you could tell us the source of the example then we could know the reliability of that mocks. And I think it would be better if we notify the author of that mock about this also.

Kaps
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kapil, i found this question in quick searching on google search. It was some discussions on Java exam topics and was not exactly exam question. As it was not a mock exam, i did not bother to note website name. If i found it again, will post link here..Sorry about inconvenience
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All,

Consider the example provided by Nitin , I have modified the code to see what it does :



class tst extends tst1{

public tst(){
System.out.println("Inside the con");
}

public static void main (String st[]) {
new tst();
}
}

class tst1
{
int i;
tst1(){
System.out.println("Called by default constructor!!");
met1();
}

void met1()
{
System.out.println("Variable i initialized without default constructor i= "+i);
}
}


Point to be Noted : Whenever an object of the subclass is created an object of the superclass is also created.

case 1:
if no constructor was provided in the subclass and no constructor was provided by the superclass then both the classes would have a default constructors provided by the compiler and the default constructor of the subclass would call the default constructor of the superclass.

case 2:
if the subclass does not have any constructor while the superclass has no argument constructor, while creating the object of the subclass using subclass(), the default constructor provided by the compiler would call super() and hence the no argument constructor is called.


case 3:
if the subclass has got the no argument constructor provided and also the superclass inspite of the fact that super() is not being called while creation of the subclass, the superclass no argument constructor is called.

case 4:
if the subclass has got no argument constructor while the superclass does not have the no argument constructor( while it has got other constructors) and if we do not call any superclass constructors from the subclass constructor the compiler would give a exception since it cannot create a superclass object

case 5:
if the subclass has no argument constructor as well as the superclass but both of them have got other constructors, make sure that the subclass constructors calls the superclass constructors since without these superclass object would not be created.

Hope this might be of some help to u..

Cheers,
Murali
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic