• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Marcus Green's Tutorial

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this question is marcus green's tutorial:
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 = 2) Compile time error
An error occurs when the class Severn attempts to call the zero parameter constructor in the class Base
According to me, the correct answer should have been 3, since void makes the constructor Severn into a method with the same name as the class. So nothing is printed. It does not even call the Base class.
Some one please help.
Thank You,
Kamil.
 
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kamil,
yes, but if in the class Severn there is no constructor the compiler will create the default constructor which allways calls the super-class constructor without arguments. So there is no zero - argument constructor in the base class, code will not compile.

Axel
SCJP2
 
Kamil Dada
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohh ok fine!
Thanks a lot!
I was confused with this question and many other questions were similar to this one.
So basically it is better if we make a default, no argument constructor for a class if we make a constructor that takes arguments.
Thank You,
Kamil.
 
Axel Janssen
Ranch Hand
Posts: 2166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. I think without being a Java Design expert (I am trying to get one). I think the user of your classes (the one who extends your classes) does not expect that he HAS TO start a constructor of an extending class with super(SomeStuff).
Axel
 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try compiling the code yourself?
Marcus
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marcus,
I compiled the code and got the error as---
Severn.java:8: cannot resolve symbol
symbol : constructor Base ()
location: class Base
class Severn extends Base{
^
1 error
But i didn't understand the meaning of ur question?
Rashmi
 
reply
    Bookmark Topic Watch Topic
  • New Topic