This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 4420
    
    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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Construcors
 
Similar Threads
marcus Green question
Mock of Marcus Green
Competition between “this”and “super” in constructor
MarcusMock No - 2 - Question 53.
is it righ?