Java - Things to know before your OCJP exam - Overridding

1. Overriding Method should have same syntax and return type as Overridden method. If the number and type of arguments varies, it becomes overloading. If the return type is different, compiler gives compilation error i.e "The return type is incompatible with BaseClass.overriddenMethod()".

2. No Explicit cast is required in case of Upcasting i.e derived class reference is assigned to Base class reference but explicit cast is required in case of Downcasting i.e Base Class reference is assigned to Derived class reference.

3. Downcasting may throw ClassCastException if object is found to be non compatible during runtime. That's why instanceof check should be made before doing the downcast.

4. Overridding method cannot throw checked exception wider than the exception thrown by the overriden method. If the overridden method doesn't throw any exception, overriding method can throw any exception.   

5. We cannot reduce the visibility of the overridden method. Means If the method in base class is public, we cannot declare it private in derived class.

6. Only instance methods can be overridden. Static methods and instance / static variables cannot be overridden.

7. Final methods cannot be overridden.

8. If a method cannot be inherited, It cannot be overridden. So private methods cannot be overridden.