Wednesday, April 10, 2013

Overriding rules in Java



  • Should be same method name and argument list in base class and derived class
  • Return type can be co-variant, meaning if super class return type is Map, subclass return type can be Hash Map.
  • Access specifier in the derived class should be broader/bigger than that in the base class;
    • ie; if base class method is protected, the derived class method can  be protected or public- but cannot be private or default
  • Exception in the subclass  should be same or subclass of base class
    • ie:-if base class exception is IOException, subclass can be FilenotFoundException-but cannot be Exception.

No comments:

Post a Comment