[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

Where to copy the functions that to be implemented

Hi,

When use "implements", we need to include all the functions something like:
public class MyDrawing extends Frame implements WindowListener{
      public MyDrawing(){
      super("MyDrawing");
      addWindowListener(this);
      }
    public void windowOpened(WindowEvent e)  {  }  //need to include all the functions even not use them.
    public  .......
    public void windowClosing(WindowEvent e)        { System.exit(0); }
    ......
I want to know where to copy those functions. I am using Eclipse3.0.
0
wellwet
Asked:
wellwet
  • 7
  • 4
  • 2
  • +2
3 Solutions
 
CEHJCommented:
Inside the opening and closing braces of that class that implements the interface
0
 
wellwetAuthor Commented:
Sorry, I didn't explain clearly.

Say, I need to do
public class MyDrawing extends Frame implements WindowListener{
  public void windowOpened(WindowEvent e)  {  }
  public  .......
  public ......
  public .......
I cannot remember the above functions' names:
windowOpened
WindowClosed
WindowClosing
.......
Where I can copy them from?
0
 
mkatmonkeyCommented:
The direct answer to your question where to find the methods, you could crack open the source code included in your JDK or you could hold down "Ctrl" and click "WindowListener". However, if  you just want to get going with actually implementing this class there are two good ways to go. Eclipse will auto-generate all the stubs that you need. Just open the context menu on the red line next to your code: "public class MyDrawing". Choose the option to "add unimplemented methods". The other option is to extend the WindowAdapter. This class already has stub methods defined so all you need to do is override the one or two methods that you need. Eclipse will indicate that you got the method signature correct by showing a blue triangle left of it.

Happy coding man.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
CPColinCommented:
In Eclipse, you can right-click in a source file, then click the Source sub-menu, then Override/Implement Methods...

It'll give you a list of the methods in the class your class derives from and interfaces your class implements.
0
 
wellwetAuthor Commented:
Thanks a lot for all helps from all experts above!

mkatmonkey,
>Just open the context menu on the red line
I just cannot find any red line nor context menu :( All I can see are Package Explorer and the edit window.

>WindowAdapter
How Java knows what methods I need? Or, I need to know what is included in WindowAdapter or orhteAdapter?
0
 
CEHJCommented:
>>How Java knows what methods I need? Or, I need to know what is included in WindowAdapter or orhteAdapter?
      
It knows what methods you're missing since you've told it you'll implement one or more interfaces. When it can't find the methods of that interface, it complains
0
 
mkatmonkeyCommented:
>just cannot find any red line nor context menu :( All I can see are Package Explorer and the edit window.
Be sure that your in the "Java" Perspective. You should see "Java"  in the upper RH corner.

>How Java knows what methods I need? Or, I need to know what is included in WindowAdapter or orhteAdapter?

You generally do not need to know what's included in an adapter since it would probably be identical to the stubs that Eclipse would generate automatically.  The point is to prevent carpal tunnel while increasing the clairity of your code. You won't have to worry about the stubs and any other developer looking at your code will not be distracted by those same stubs.
0
 
mkatmonkeyCommented:
One more thing: Eclipse may not be validating your Java code since your code is not part of an Eclipse Java project. Try creating a new project setting your home to be the same directory where your class is.
0
 
wellwetAuthor Commented:
Yes, you are right, I have just found the context menu. Thank you!

About the WindowAdapter:
I extends WindowAdapter because I want to use method WindowClosing() and I know WindowClosing() is included in WindowAdapter. However, if I want to use method WindowClosed(), how can I know if WindowClosed() is included in WindowAdapter or not?
0
 
mkatmonkeyCommented:
All the methods are in WindowListener are implemented by the WindowAdapter to do nothing. You can actually see what methods the WindowAdapter implements by holding down the "Ctrl" key and clicking on WindowAdapter in your code. Eclipse will actually semi-decompile the class and show you the methods in WindowAdapter.

In order to "use" the Closing method you must override it by defining your own version.

0
 
wellwetAuthor Commented:
I see, WindowListener' methods == WindowAdapter's methods, except that using WindowListener we must write all its methods in the code while using WindowAdapter we needn't write the methods we don't need.

However, It seems not works:
public class MyDrawing extends Frame, WindowAdapter{
      public MyDrawing(){
      super("MyDrawing");
      addWindowAdapter(this);
      }
      public void windowClosing(WindowEvent e)   { System.exit(0); }
0
 
CPColinCommented:
That is correct; in Java, you can implement as many interfaces as you want, but you may extend only one class. You'll likely have to create a separate class that extends WindowAdapter, then pass an instance of it to addWindowListener.
0
 
wellwetAuthor Commented:
OK, we may extend only one class......Thank you CPColin!

We need to pass an instance of WindowListener to addWindowListener, but why we can pass 'this' to addWindowListener? 'this' is an instance of WindowListener?
public class MyDrawing extends Frame implements WindowListener{
      public MyDrawing(){
      addWindowListener(this);
      }
0
 
objectsCommented:
this is the instance of the enclosing class, in your case your frame instance.
It (MyDrawing) implements WindowListener so you can pass it to a method that expects a WindowListener
0
 
wellwetAuthor Commented:
Thank you objects!

So, any 'big' instance, as long as its class implements WindowListener, can be used as an instance of WindowListener to pass to addWindowListener, and Java will look for the WindowListener's meshods inside the 'big' instance?
And do you think that is wasting resource by pass a 'big' instance (Frame's instance) as a 'small' instance (WindowListener's instance)?
0
 
objectsCommented:
thats correct, by implementing the interface its like signing a contract saying I will guarantee to include these methods (the ones defined in the interface)
No it not a waste passing the frame around, args get passed by value which in the case of an object is just its reference.
0
 
wellwetAuthor Commented:
OK, I need to lean the 'reference' in Java :)

Thanks a lot again for all helps from all experts here!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now