Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Listeners and Adupters

 
prasanna perera
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 43.
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
}
}
answers give: a,d
However, I think b,c are correct and d is wrong as when the class implements WindowListener the class has to declare all the methods in this listener interface ?
 
Kishan Kumar
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Answers a,c,d are correct if you admit to the typing mistake
in the line
int i = 10;//Missed semicolon. in the answer c.
b is wrong because java dosent support multiple inheritance.
(Language basics, maybe you would have forgotten )
d is correct because the class extends WindowAdapter and
implements WindowListener.
You would know that the class should implement all the methods
declared in the interface which it is implementing. But the
catch is java is happy when this class or any of its superclasses
implement the methods of the interface. In this case WindowAdapter has already implemented all the methods of the
WindowListener interface and you are overriding one of the methods and the rest you are inheriting.
Hope this helps
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic