Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class declarations

 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Anup Engineer
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently, this is obvious to everyone but me!
Why is choice B wrong?
Thanks.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class cannot extend multiple classes !
 
Anup Engineer
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thats the worst I have missed, as yet!
Thanks.
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic