aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Construcors 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 "Construcors" Watch "Construcors" New topic
Author

Construcors

Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 339
Given the following class definition which of the following can be legally placed after the comment line
//Here ?
class Base{
public Base(int i){}
}
public class MyOver extends Base{
public static void main(String arg[]){
MyOver m = new MyOver(10);
}
MyOver(int i){
super(i);
}
MyOver(String s, int i){
this(i);
//Here
}
}
1)MyOver m = new MyOver();
2)super();
3)this("Hello",10);
4)Base b = new Base(10);
The answer here is 4, but why not 1 also ???
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4419
    
    5

The compiler will provide a default (no-args) constructor only if there are no other constructors explicitly declared. Since class MyOver has two explicitly declared constructors that take arguments, the compiler does not provide a default constructor, making 1 an incorrect choice.
 
Consider Paul's rocket mass heater.
 
subject: Construcors
 
Similar Threads
marcus Green question
MarcusMock No - 2 - Question 53.
Mock of Marcus Green
is it righ?
Competition between “this”and “super” in constructor