27 September 2009

OOPS - Rules of Overriding?

This is a typical interview question. What are the rules of overriding. Here goes the answer
  1. The method in the subclass should have the same signature as that of the method in super class
  2. The method in subclass should not be more restrictive in the context of access specifiers
  3. The method that overrides the method in super class cannot be through broader exception meaning that it cannot throw the super class of exception thrown by super class.
  4. "final" method cannot be overridden
Leaving out the last condition, as it is obvious, is there a reason for top three conditions. Why does Java (or any object oriented programming language) have this as rule? Is there any logic? BTW, this is not a standard interview question. But a question that provokes thinking. Will post the answer in couple of days (meanwhile if you know the answer, write in comments)

2 comments:

உத்தண்டராமன் said...

Waiting for ur answers :)

Lakshmi said...

@Udan

How r u?

The answer is simple. why dont you try it.. i ll try to answer in couple of days probably :-)