• 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
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

Polymorphic reference and overriding

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

I thought polymorphic reference and overriding is clear to me but I got stuck when I found this. I understand the fact that always overridden method is executed in case of Polymorphic reference(Superclass reference and subclass instance). For example:-



Output will be:- Horse eats.

But when I add throws Exception with Animal eats method why does Animal eat() method is executed?


I think, that here it violates rules of overriding and hence no overriding happens here. Therefore, Superclass method is called. But I am not sure with this.

Thanks in advance
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checked exception that method invocation can throw determined by the type of reference. (this for need declare or handle)
so you must modify

Exception may be throw by super class, but for child can not throw any exception.
So second rule is: when override overrider method in sub class can`t throw new checked exception than super class not throw or more general checked exception than superclass declare to throw.
It may not throw exception, or throw more specific subclass of exeption than superclass declare
 
Enthuware Software Support
Posts: 4573
45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure you are doing in the code what you are saying here? Because what you are saying here shouldn't happen.
 
Bartender
Posts: 2235
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you add throws Exception to the Animal class the output should still be Horse eats....
Always add @Override annotation to overriden methods.
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Child4 and Child5 will not compile

for interface

you may not throw exception or what my be substitute of "???" ?
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Anilprem wrote:Are you sure you are doing in the code what you are saying here? Because what you are saying here shouldn't happen.

(if it was for my post)
I mean that because type of variable this code must be in trycatch because this determine that method invoked on this type of variable may throw checked exception declared in method declared in type (of variable), actual method will chosen depend of actual object.

i mean that

(sorry for confusing)
 
Jyoti Kaushal
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the replies. I understand the fact that if I catch the Exception then code works fine and prints Horse eats. The question which I am asking is that if I don't catch the Exception compiler error occurs :-


^

Trying to understand the exception, It looks like it didn't override the method and executed the superclass version. I expected error something like "Error in override or illegal override". I want to know how the compiler reacted in this case and gave such error? I am sorry if the question seems little weird .
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this will not compile.
and next too.
but in first example - you violate overriding rules. and in second you violate rule for checked exception that must be handled (by trycatch) or declared in signature, you make right override in second code- class Animal and Horse will compile.
Reason for this that bacause you use type of a - Animal it can be reefer to Object of Animal that in this case can throw Exception, or other subclass of Animal that can do it too, so you must declare or handle this invocation

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

The thing which I understood with your example is that issue is in the violation rule of overriding. But the issue basically is in the unhandled exception. Therefore, once I catch the exception using try catch the code works fine.
 
Sergej Smoljanov
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jyoti Kaushal wrote:Sergej,
The thing which I understood with your example is that issue is in the violation rule of overriding. But the issue basically is in the unhandled exception. Therefore, once I catch the exception using try catch the code works fine.


and if you override properly - you will run method from object you actually refer.
 
permaculture is largely about replacing oil with people. And one tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic