This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Class declarations 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 "Class declarations" Watch "Class declarations" New topic
Author

Class declarations

Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
Which of the following class declaration(s) is(are) true?
A.import java.awt.Button;
import java.lang.Runnable;
public class MyClass extends Button implements Runnable{
public void run(){

//Some valid Code
}
}
B.import java.awt.event.*;
import java.applet.Applet;
public class MyApplet extends Applet,WindowAdapter{
public void windowClosed(WindowEvent we){

//Some Valid Code
}
}

C.import java.lang.Runnable;
public class MyClass implements Runnable{
public void run(){
int i = 10
System.out.println("i = "+i);
}
}
D.import java.awt.event.*;
public class MyClass extends WindowAdapter
implements WindowListener{
public void windowClosed(WindowEvent we){
//Some Valid Code
}
}
I have answered A and C, but the site says answer is A and D.
The Choice D, i think is wrong because when we are implementing an Interface then all its methods are to be overridden other wise the class is to be declared abstract
Can anyone say why choice C is wrong, i dont see anything wrong in it.
Please clear
Thanks in advance
kareem
:roll:
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
C is wrong because this line:
int i = 10
has no semicolon after it.
D is fine. You are extending WindowAdapter,which is a concrete class, and contains implementations for all the methods in the WindowListener class, which WindowAdapter implements.
It's redundant to declare that MyClass implements WindowListner, because it already does so indirectly via its superclass WindowAdapter. However, even though it's redundant, it's not an error.


Rob
SCJP 1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
in choice C: If you look carefully, a semicolon is missing on line int i=10
This is mean, though !
in choice D: MyClass extends WindowAdapter which already implements WindowListener, and thus, MyClass also implicitely implements WindowListener by inheriting WindowAdapter methods. There is no problem with that code.
Rob,
WindowAdapter is an abstract class although no methods in it are abstract. This is done that way so that developer actually subclass WindowAdapter.
[ March 15, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
Apparently, this is obvious to everyone but me!
Why is choice B wrong?
Thanks.


Anup Engineer
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
A class cannot extend multiple classes !
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
Thats the worst I have missed, as yet!
Thanks.
Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
Hey that was pretty silly i missed the ;
also thanks for reminding me that Adapter classes are actually implementations of the Listener interfaces
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
In the real exam, you won't need to make sure that no semicolons are missing because the focus is mainly on Java concepts and not on syntax issues.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class declarations