• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Polymorphism! correct me if i am wrong.

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i have a polymorphic reference to refer to a subtype object with an overriding method, the complier just checks whether the method is present in the super class or not.

But the JVM comes into picture while runtime and invokes the subtype method ( for which the object was actually created )

Now I see an example where the superclass method declares a checked exception in the method but the sub class method( overriding method) doesn't.
Now if the superclass reference is to refer the subtype object with the overriding method, I see a compiler error - unreported exception. Why is this..? Is it because the complier just checks for the "presence " of the method in the super class..?

I am confused..!
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It would actually help if you posted a small example -- as I (and maybe others) am not exactly following you.

Henry
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Example taken from K&B SCJP book.
 
Henry Wong
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I see what you are getting at.

Sanjana Sharma wrote:
Now I see an example where the superclass method declares a checked exception in the method but the sub class method( overriding method) doesn't.



This is perfectly legal, because throwing less exceptions doesn't mean that a subclass isn't IS-A super class. Maybe the subclass has error handling so doesn't need to throw that exception.

Sanjana Sharma wrote:
Now if the superclass reference is to refer the subtype object with the overriding method, I see a compiler error - unreported exception. Why is this..? Is it because the complier just checks for the "presence " of the method in the super class..?



Basically, there is a reason that you are using the super class reference right? Maybe you need to call code that doesn't know about the subclass. Regardless, it has to behave like the superclass. So, the compiler treats it as such..... Or you can say the compiler isn't smart enough.

Henry
 
Sanjana Sharma
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, i get it.

But if there was no exception declared at all..
The complier doesn't complaint right?

Here the overriding methods gets invoked ( JVM checks for the actual object type) ..
 
Oh sure, it's a tiny ad, but under the right circumstances, it gets bigger.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic