• 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 about Constructors on Marcus Green's Mock Exam #3

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
On Marcus Green's Mock Exam #3 Question 11 is as follows:

What will happen when you attempt to compile and run the following code
class Base{
public void Base(){
System.out.println("Base");
}
}
public class In extends Base{
public static void main(String argv[]){
In i=new In();
}
}
1) Compile time error Base is a keyword
2) Compilation and no output at runtime
3) Output of Base
4) Runtime error Base has no valid constructor
// End of Sample Code
I chose 3 because I was under the impression that even though the subclass "In" has no constructor the JVM would automatically make one.
My first question is will the JVM make a default contructor?
Second,
I was also under the impression that when an object of a derived class is instantiated, the superclass constructor is called first. In this case the constructor for class "Base" would be called thereby printing out the string Base.
Is this not correct also?
Thanks in advance,
B Barnett

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
B,
A default constructor will be created for both Base and In classes. A default is created for Base because a constructor does not have a return type and the Base() method of class Base returns void.
Also, when In is instantiated the first line of its default constructor will implicitly call the empty default contructor for Base which will result in no output. Therefore, 2 will be correct.
Hope this helps,
Bob Kerfoot - SCJP
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hi B,
your question is
What will happen when you attempt to compile and run the following code
1. class Base{
2. public void Base(){
3. System.out.println("Base");
4. }
5. }
6. public class In extends Base{
7. public static void main(String argv[]){
8. In i=new In();
9. }
10. }
ans.2) Compilation and no output at runtime
remove void from line 2 and you will get ans.3)Output of Base
because look carefully at line 2 it is method Base() with void return( it is not constructor as contructor doesn't have any return type)
yes compiler give us default constructor at no cost
(absolutly free !)if and only if class does not have any constructor and it is
class Base{
Base(){
super();
}
}.
access modifier of constructor will be same as that of class. in this case it is default.
Hope this helps,

regards,
ashok
 
B Barnett
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ashok,
Thanks, I get it now.
- B Barnett
 
reply
    Bookmark Topic Watch Topic
  • New Topic