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

Marcus Green Mock exam Question # 16

 
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen when you attempt to compile and run the following class?
class Base{
Base(int i){
System.out.println("Base");
}
}
class Severn extends Base{
public static void main(String argv[]){
Severn s = new Severn();
}
void Severn(){
System.out.println("Severn");
}
}
1) Compilation and output of the string "Severn" at runtime
2) Compile time error
3) Compilation and no output at runtime
4) Compilation and output of the string "Base"

Answer is 2.
Answer explanation given is : An error occurs when the class Severn attempts to call the zero parameter constructor in the class Base
Can any one please explain , where in the program the class Severn attempts to call the zero parameter constructor in the class Base . And also explain why is that answer is 2 , why is not 3.
Thanks,
Sdev.

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

Originally posted by sdev:
Can any one please explain , where in the program the class Severn attempts to call the zero parameter constructor in the class Base . And also explain why is that answer is 2 , why is not 3.
[/B]


The program attempts to call the zero parameter constructor in the default constructor which is provided automatically in absense of any constructor :
Servern() { super(); }
NOTE : void Servern() is not a constructor.
Since no zero parameter constructor exists in class Base, it gives an compilation error !
Tricky one !!! ;-)
Deepak

[This message has been edited by Deepak M (edited July 16, 2000).]
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sdev,
The class Severn has no constructors (void Severn() is just another method and not the constructor). Now the compiler makes the default no argument constructor available to the class Severn. The first line of this constructor is super();. This will call its base class'es no argument constructor. Now in the Base class there exists no constructor having the signature Base(), therefore the error. Note here that since one constructor for the Base class has been provided by you ( Base(int i)), no default constructors are created for this class.
Hope this clears your doubt.
Jayesh
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sdev,
In the statement Severn s = new Severn(); s is the new object formed. But, even before s starts to get created all the corresponding constructors(not always corresponding, sometimes default too!!) in the inheritance hierarchy need to finish executing. Now, in the Base class there is a constructor provided which takes an arg type int. If the object s was being instantiated by a constructor which took int as an arg then it would have been ok(you would have to provide the constructor defn for Severn(int i)).
When there is no explicit declaration of any constructor in a class the default (no args) constructor is provided by default. So, when "s" is being instantiated it looks for the constructor Base() in the parent class. But, that is not there because another constructor is already defined and therefore Base() constructor is not provided by default.
When you are saying Severn s = new Severn(); where is the Severn() constructor ?? It is being provided by default!!
It has been a pretty long explaination. Hope it has been of some use!!
Ankur
 
sasank manohar
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thank you Deepak, Jayesh and Ankur for ur answers. All of ur answers helped me to get out of this coufusion. I went through many books but , really I couldn't make out the clear picture of it.
I really appreciate all of ur answers
Thank you very much...sdev
 
pie. tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic