This week's book giveaway is in the Design forum.
We're giving away four copies of Experimentation for Engineers: From A/B testing to Bayesian optimization and have David Sweet on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Using super to call overriden method

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for explaining me.
 
I've never won anything before. Not even a tiny ad:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic