Saturday, November 17, 2012

A convenient way to override methods


We use @Override annotations to declare for overriding methods on sub classes. Normally, we need a new class inherit from interfaces or super classes. However, in some cases, we just want to change only one method one time, there is a better and more convenient way to do it than creation a new class.

The @Override annotation

It is not required to use this annotation for overriding methods. Your code is still fine without it (if you implement right).

class MyTextField extends JTextField {
     public String getSelectedText() {
          return super.getSelectedText().toUpperCase();
     }
}

This code is compiled successful. So why do we need the @Override annotation? It guarantees that you override on the right way. Programmers are also human who could not avoid some typing mistakes. For the example above, what will happen if we type getSelectedText to become getSelectText or getSelectionText?  If we type wrong, when a MyTextField object invokes getSelectedText() method, the result is the same with getSelectedText() of JTextField. The @Override annotation helps us to avoid this problem. It will check overriding methods with overridden methods. If they do not match, it will be a compile time error. It is much better a runtime error or even it is more dangerous and difficult to find out if it works fine but wrong results when we do not use @Override.

class MyTextField extends JTextField {
     @Override
     public String getSelectedText() {
          return super.getSelectedText().toUpperCase();
     }
}

When we use @Override, if we type getSelectionText, IDE will show an error at the line of declare method.

A convenient way to override methods

In the situation we just want to change only one method from a class on the specific case, it is not a good way if we create a new class extending that class. We could create an instance with hot-overriding way.
Normally, we often do like this when we want to implement interfaces. The common examples are *Listener interfaces. (Java Swing, Android, etc). When we want to add a listener for a button, or listView, we could add a new instance of interfaces. 


          JButton bOk = new JButton();
          bOk.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent arg0) {
                   // TODO Auto-generated method stub
              }
          });

The interesting thing is there are not too many people realize that we also could do it with classes. For instance, if we just want to create only one JTextField having a new getSelectedText(), we could do:

          JTextField textField = new JTextField(){
              @Override
              public String getSelectedText() {
                   return super.getSelectedText().toUpperCase();
              }
          };

The textField variable referees to a new JTextField which has only one change with the original. Obviously, we do not to create a new class and we could easily reduce the complexity of our code. This convenient way is more suitable for classes including so many methods and sometimes we just want to change one of them.

No comments:

Post a Comment