• 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

Overriding Issue

 
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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


 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • 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
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this code will not compile,
as Line 10 throws an IOException, which is a checked exception and the method OverLoadOrOverRideChild1.show6() does not declare that in its throws clause.

Regards,
Flom
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was the complete code I tried



Now this code is compiling and giving me an output of

2
Hi from show6 Child
Exception in thread "main" java.lang.ArithmeticException
at OverLoadOrOverRideChild.show6(OverLoadOrOverRideDemo.java:65)
at OverLoadOrOverRideDemo.main(OverLoadOrOverRideDemo.java:79)
Press any key to continue . . .

What do you say?

Then I added these 2 lines

p=new OverLoadOrOverRideChild();
p.show6();

and compiled
it gave me compiler error

G:\SaharaMyMachine\EDrive\SCJP\KB\chapter2\OverLoadOrOverRideDemo.java:81: unreported exception java.io.IOException; must be caught or declared to be thrown
p.show6();
^
1 error

Process completed with exit code 1

Can you explain.

Its importatnt as I'm preparing for SCJP.
Regards,
Nancy
 
Flom Xanther
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, as the compiler tells you..

You have either to catch the thrown exception or you have to declare, that the calling-method (the main-method) is able to throw that Exception.

Regards,
Flom
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My doubt is without those 2 lines why code compiles and doesnot give me an error.

Regards,
Nancy
 
Flom Xanther
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this two lines your are using OverLoadOrOverRideParent as the reference-type,
and OverLoadOrOverRideParent.show6() declares that it throws an IOException.

If you are using OverLoadOrOverRideChild as the reference-type, it compiles because OverLoadOrOverRideChild.show6() does not declare that it throws any exceptions!

If your are using the Parent-Type you are "talking" to the methods of the parent at compile-time (polymorphically the subclass-children-methods will be called at runtime)

So, you have to take care of the method-signature and throws-clauses of the parent-type (if you are using that reference-type).

Regards,
Flom
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Flom, Now its clear. So its a regular checked exception issue and nothing to do with overriding at this stage.

Regards,
Nancy
 
Flom Xanther
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes you're right.


Regards,
Flom
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for confirming.

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

Nancy Antony wrote:Thanks for confirming.

Regards,
Nancy



Hi Nancy/Flom,
In chapter 2, under Overriding & Overloading, Differences Between Overloaded and Overridden Methods is given. As per my understanding, for Overriding methods, the reference types method signature will be considered at compile witm and during runtime, the object types method signature will be considered.

For Overloading methods, during compile time, the object types method signature will be considered during runtime. Can anybody explain the below text clearly for overloaded methods.Its confusing:


Reference type determines which overloaded version (based
on declared argument types) is selected. Happens at compile
time. The actual method that’s invoked is still a virtual method
invocation that happens at runtime, but the compiler will
already know the signature of the method to be invoked. So at
runtime, the argument match will already have been nailed
down, just not the class in which the method lives.



Thanks in advance.
 
Nancy Antony
Ranch Hand
Posts: 145
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mahalakshmi, I think your perception is accurate.

Regards,
Nancy
 
Mahalakshmi Chandru
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Nancy..this makes me feel more optimistic
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic