• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Applied Reasoning mock exam question

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a problem with one of the applied reasoning questions. The question reads like this:
Select each of the code fragments that would allow a newly created java.awt.Button to generate ActionEvents:
a. button.enableActionEvents();
b. button.addActionListener(anActionListener);
c. button.enableEvents(true);
d. button.enableEvents(AWTEvent.ACTION_EVENT_MASK);
I answered both b and d. He suggetst b is the only correct answer, and the reasoning for d not being correct is that , since enableEvents is protected, it can only be called by a subclass. Since enableEvents is defined in Component, and since Button derives from Component, shouldn't it be able to call it? Also, looking at the Exam Cram Sun Certified Java Programmer book, they have an example that is almost identical to this.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Peter,
The correct answer is b and only b.
Choices a and c are not viable choices because they don't match any existing method signatures.
Why is d not a valid answer?
A protected method can only be called from the class in which it was created (java.awt.Button) and any subclasses of it (extends java.awt.Button).
The question states: a newly created java.awt.Button which rules out a subclass of java.awt.Button and the Button code itself. Knowing this we can say that d is not a valid choice.
Or arguing the other way we can say that the only way that I can create a java.awt.Button is with the new operator. By using this, I have not subclassed it or changed its code, therefore, I can not call any protected code.
Another great argument is to code it up and see that the compiler will not even let you get away with calling a protected method without being a subclass or the originating class.
Regards,
Manfred.
 
Peter Chirco
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, I did some coding, and I am getting the results you mentioned, but I am still not sure why. I created a frame, and added 2 components to it: a Button, and a myButton, which I defined. I first tried to call button1.enableEvents(), and I got an error telling me that method was not visible. That is what you told me to expect. I then used the usual process, added an ActionListener, and got it to work fine. For myButton, I called enableEvents() in the constructor, and then overrode the processActionEvent() method. I added that to my frame, and it works also. So I have a frame with two buttons, both of which respond to action events, one through an event listener and one through explicit event enabling.
I still have the question, though, why I could not call button1.enableEvents(). since the method is defined in Component, as a protected final method, I should be able to call that method from any subclass of Component, or any class in the same package as Component.
import java.awt.*;
import java.awt.event.*;
public class ActionTest extends Frame implements ActionListener
{
class windowHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
Button button1;
MyButton button2;
public ActionTest()
{
addWindowListener(new windowHandler());
button1 = new Button("Button");
button2 = new MyButton("MyButton");
setLayout(new GridLayout(1,2));
add(button1);
add(button2);
// button1.enableEvents(AWTEvent.ACTION_EVENT_MASK);
button1.addActionListener(this);
}
public void actionPerformed(ActionEvent ae)
{
Button tempbutton = (Button)ae.getSource();
if(tempbutton == button1)
System.out.println("Button 1");
}
public static void main(String[] args)
{
ActionTest at = new ActionTest();
at.setBounds(10, 10, 50, 200);
at.show();
}
}
reply
    Bookmark Topic Watch Topic
  • New Topic