This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you�re calling the supertype version of the method. If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception (more in Chapter 5). Let�s take a look at an example:
Can anyone please explain why did a.eat(), gave error, as it sentence tells the complier treats as if you were calling the superclass method. Please advise...
"a" is a Animal referance type. Hence if you call any method using referance "a" compiler will assume you are calling a method in an Animal Object. Now because eat() method in Animal Object throws an Exception so we need to handle or declare this exception in our main method. Compiler has no knowledge that "a" actually referances a Dog2 Object.
Which method to call is determined at run time based on object type rather than reference type.
And you must be aware that we can only call overrriden methods from sub class using reference of super class. This is quite logical because compiler do not know at compile time what object is being referenced by super class reference. So at compile time it assumes you are calling the method of super class using super class reference(irrespective of object type) and hence it gives compile time error.
Sanket is right. That 'a' refer to the supertype at the compilation time , so when you say a.eat() its consider the method declare in Supertype. Becouse of declaration of Checked Exception for method eat() in superType, You have to catch the Exception or make the Main() method declare to be thrown
Compile time error need to solve first to make the code able to Run and then only it can be participate in Polymorphisam.
Thanks & Regards Poonam Agarwal
Thanks <br />Poonam Agarwal SCWCD 86%
Joined: Aug 06, 2007
As the method is overriden, you mean to say that if super class throws the checked excpetion, base class should also throw the exception. But i have heard that the overriden methods throws unchecked exceptions i.e runtime not the checked exceptions.
Can you little bit elaborate and explain ...
Joined: Jan 15, 2007
"if super class throws the checked excpetion, base class should also throw the exception"
I assume, you made a typo mistake here and what you wanted to say is "if super class throws the checked excpetion, then sub class should also throw the exception"
Yes, this is correct.
The overriden method in the subclass can only throw a subset of the checked exception classes(including their subclasses) thrown by the inherited method in the superclass. This means that an overriding method cannot allow more checked exceptions in its throws clause than the inherited method does.
Allowing more checked exceptions in the overriding method would create problems for clients who already deal with the exceptions specified in the inherited method. Such clients would be ill prepared if an object of the subclass (under the guise of polymorphism) threw a checked exception they were not prepared for.
I hope I have not confused you more#
To summarize, if the super class method throws any checked exception then coresponding sub class method can only throw following type of exceptions: 1. Do not throw any exception 2. Throws all exceptions which are thrown by super class method 3. Throws any exceptions which are sub type of any exceptions thrown by super class method 4. Any run time exception
In all these cases, the code to call this method must be enclosed by try/catch with catch block for all the checked exceptions thrown by super class method.