Tuesday, June 15, 2010

Overriding and Hiding

Overloading
More than one methods have the same name but different signatures

Overriding
Replacing the implementation of a methods in the superclass with one of your own.
  • You can only override a method with the same signature.
  • You can only override non-static methods.
Hiding
Fields and static methods can not be overridden. They can
only be hidden.
  • Hidden fields and static methods are still accessible via references to the superclass.
  • A static method can be only be hidden by another static method.
  • A static variable may be hidden by an instance variable.

Which implementation is used?
  • When invoking a non-static method, the actual class of the object determines. (run-time)
  • When accessing a field, the declared type determines. (compile time)
  • When invoking a static method, the declared type determines. (compile time)

0 comments:

Post a Comment