Which is the Best Java Web Framework ?

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.

Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus


Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.