Overriding Methods




Overriding Methods involves writing a method in a subclass that has the same exact name (identifier), same parameters, and same return type as a super class method.

Examples:


Overriden methods appear in many (if not all) of the built in classes.
For example, in the Object class, there is a toString() method.
All classes extend the Object class either directly or indirectly.
All of the wrapper classes (Integer, Double, Float, Short, etc.) and 
the String class have their own toString() method.  


Object s = "abc";
System.out.println(s.toString()); // String toString() method is called

Object t = new Integer(7); // or just Object t = 7;
System.out.println(t); // Integer toString() method is called

There are certain methods that you should always override in your own classes
(like toString() and  equals()).



  • A Good Website