aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using super to call overriden method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using super to call overriden method" Watch "Using super to call overriden method" New topic
Author

Using super to call overriden method

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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...


Thanks<br />Dinesh
jaspreet atwal
Ranch Hand

Joined: Sep 05, 2007
Posts: 52
Dinesh,

"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.


Still Learing..
Sanket Meghani
Greenhorn

Joined: Jan 15, 2007
Posts: 14
Well,

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.

Hope this helps.

Regards,
Sanket
Poonam Agarwal
Ranch Hand

Joined: May 12, 2008
Posts: 323
Hi ,

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%
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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 ...
Sanket Meghani
Greenhorn

Joined: Jan 15, 2007
Posts: 14
"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.

Just learned a new internet acronym! HTH

Regards,
Sanket
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Thanks a lot for explaining me.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Using super to call overriden method