Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

a qn from RHE..

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
please look at the foll code:

The options are :
1 Adding a window listener to an instance of MyFrame will result in a compiler error .
2 Adding a window listener to an instance of MyFrame will result in throwing an error at Runtime.
3 Adding a window listener to an instance of MyFrame will result in code that compiles cleanly and executes without throwing an exception.
4 A window listener added to an instance of MYFrame will never receive notification of window events.
the answers given are C and D
D is correct,but I doubted whether option C would make the compiler ask for implementation of the rest of the methods in the interface, and modified the code like so..

and, the compiler did scream !
isn't option D the only answer, or am I missing something?
thanks in advance,
Vidya.
 
vidya
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean,shouldn't A and D be the answer?
Vidya.
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see this code
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame
{ MyFrame mine ;
public MyFrame(String title) {
super(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
//mine.addWindowListener(new WindowListener(){});
aa1 x= new aa1();
addWindowListener(x);


}
public void processWindowEvent(WindowEvent we) { System.out.println("processing window event");
}
}
public class aa1 extends WindowAdapter{
public static void main(String args[]){
Frame frame=new MyFrame("hello");
frame.show();
}
}
AND
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame
{ MyFrame mine ;
public MyFrame(String title) {
super(title);
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
//mine.addWindowListener(new WindowListener(){});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{ System.exit(1);}
});

}
public void processWindowEvent(WindowEvent we) { System.out.println("processing window event");
}
}
public class aa{
public static void main(String args[]){
Frame frame=new MyFrame("hello");
frame.show();
}
}

you can add window listener class into anonymous inner class of the frame.

so
C,D are correct
jaideep
 
vidya
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh well,yes Jaideep , you are right .
I forgot about the adapter classes..
Thanks for that

Vidya.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic