Results 1 to 3 of 3
  1. #1
    Unsub's Avatar
    Unsub is offline Member
    Join Date
    Jan 2010
    Posts
    30
    Rep Power
    0

    Default @Override Annotation

    what is the purpse of the @Override Annotation?

  2. #2
    [RaIdEn] is offline Senior Member
    Join Date
    Oct 2009
    Location
    California,US
    Posts
    201
    Rep Power
    5

    Default

    it just specifies your abt to override that method.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    When you do this, you are asking the compiler to make sure that the method that follows is truly present in the super class, that you are in fact overriding a super method. For instance, if you do any Swing coding, you'll often find yourself overriding a JComponent's (such as a JPanel's) paintComponent method. If you try do do this on a JFrame object like so (which I don't recommend you do):

    Java Code:
    public class FuSwing1 extends JFrame {
      
      public void paintComponent(Graphics g) {
        g.drawLine(10, 10, 50, 50);
      }
    }
    Your program will compile fine and run fine, but since JFrame's don't have the paintComponent method, your method above won't be called, a line won't be drawn, and you may be scratching your head trying to figure out what went wrong.

    If on the other hand, you used the @Override annotation like so:

    Java Code:
    public class FuSwing1 extends JFrame {
      @Override
      public void paintComponent(Graphics g) {
        g.drawLine(10, 10, 50, 50);
      }
    }
    The compiler will complain that you are not overriding an existing JFrame method, and you'll be able to spot (and hopefully) fix your error before even running the program.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •