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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Overriding problem

 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Ranchers,

See the code below



If I comment lines 4 and 5 it calls the show6() of child class
But when I include lines 4 and 5 it gives compile time error unreported exception

Now my confusion is when parent refers the child object which is already created, it runs fine. But when I create Child Object and on the left parent object ref is present then it gives error. Why?

Regards,
Nancy

(Jesper Young: Added code tags - please click the link to learn about code tags and how to use them).
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please use code tags. Un-formatted code is hard to read.
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
what are code tags?
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
First go to the edit screen () and then in the edit page use the button "Code" to include all your code inside it.
 
Ranch Hand
Posts: 757
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

Nancy Antony wrote:what are code tags?



Look at the below example on using code tags:



When typing posting a code fragment, please use the "Code" button to let it to be rendered as a 'code'. And see it is important to use code tags.
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks for explaining

I've posted it once again using code tag.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Hi Nancy,

Are you sure that your code compiled when you commented the lines 4 and 5? It did not compile fine when I tried to do it.

It gave an error saying that the show6() method in the child class must throw IOException or it must be caught. After commenting the line throw new IOException(); your code compiled fine.

Now when the lines 4 and 5 are uncommented then there you are trying to implement polymorphism which is a runtime phenomenon. So at compile time the Parent class show6() method is referred and hence the compiler gives the error.

To compile that code you need to put the p.show6() code in a try catch block or declare the main method as throwing the IOException.

Hope this helps you.

Thanks,
Hemnath
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator

I've posted it once again using code tag.



Where? It's not edited in your original post. Or somewhere else?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
You don't need to repost the whole question just to add code tags. You can edit your own posts by clicking the button on the top right of your post.

I added the code tags for you (I can do that because I'm a moderator in this forum).

I see your repost is here. Please continue there, I'm closing this topic.
 
    Bookmark Topic Watch Topic
  • New Topic