File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Green's Tutorial

 
Kamil Dada
Ranch Hand
Posts: 43
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Axel Janssen
Ranch Hand
Posts: 2166
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Marcus Green
arch rival
Rancher
Posts: 2813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try compiling the code yourself?
Marcus
 
Rashmi Tambe
Ranch Hand
Posts: 418
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic