File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Marcus Green's Tutorial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Marcus Green Watch "Marcus Green New topic
Author

Marcus Green's Tutorial

Kamil Dada
Ranch Hand

Joined: Aug 07, 2001
Posts: 43
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

Joined: Jan 08, 2001
Posts: 2164
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

Joined: Aug 07, 2001
Posts: 43
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

Joined: Jan 08, 2001
Posts: 2164
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

Joined: Sep 14, 1999
Posts: 2813
Did you try compiling the code yourself?
Marcus


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Marcus Green's Tutorial
 
Similar Threads
Default constructor !!!!
Inheritance
Marcus Green Mock exam Question # 16
method with name of constructor
constructor