This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Jargon mock exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Jargon mock exam" Watch "Jargon mock exam" New topic
Author

Jargon mock exam

rubna
Greenhorn

Joined: Jun 15, 2000
Posts: 25
I have a small doubt here about the correct answer
class Base
{
void test() {
System.out.println("Base.test()");
}

}

public class Child extends Base {

Child(int i) { test (); }

Child(float f) { this ((int)f); }

void test() {
System.out.println("Child.test()");
}

static public void main(String[] a) {
new Child(10.8f).test();
}
}
The correct answer is Child.test() Child.test().
I compiled it too & got the same answer.
But when we call the constructor Child(float f),does'nt it try to call the default base class constructor?I thought that it would give the compiler error by saying that there's no match for base class constructor super()?Can anybody please verify this for me?
Thanks,
rubna
Herbert Maosa
Ranch Hand

Joined: May 03, 2000
Posts: 289
Hie Rubna,
Your question :
-------------------------------
I thought that it would give the compiler error by saying that there's no match for base class constructor super()?Can anybody please verify this for me?
-------------------------------------
If you do not supply a constructor for your class, An implicit constructor is provided for you, called the default constructor.The default constructor takes no arguments and all it does is to call super().So in your base class you did not provide a constructor and as such you get this default construtor.So you dont get that error because it is this default constructor that answers super() when you create new Child(10.8).
Regards,
Herbert.
rubna
Greenhorn

Joined: Jun 15, 2000
Posts: 25
Originally posted by Herbert Maosa:
Hie Rubna,
Your question :

Thanks a lot Herbert.U made it clear for me.
sgwbutcher
Ranch Hand

Joined: May 13, 2000
Posts: 56
This is true as far as it goes but don't forget that if a parameterized constructor exists in the base class, you have to provide the non-parameter constructor manually, the compiler won't give you the free default constructor anymore.
If Base was defined as:
class Base {
Base( int i ) {
int x = i; // something stupid
}
// now needs no-parameter constructor for subclassing
void test() {
System.out.println( "Base.test()" );
}
}
Then you *would* have gotten a compiler error.

Steve Butcher
exceptionraised@aol.com


-------------------------<BR>Steve<BR>sgwbutcher@aol.com<BR>http://hometown.aol.com/sgwbutcher/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Jargon mock exam
 
Similar Threads
mock exam Jargon(sarga)
super in constructors
Constructor
Confused help me!!
java programmer certificate