Tuesday, June 15, 2010

Overriding and Hiding

More than one methods have the same name but different signatures

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.
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)

No comments:

Post a Comment