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.
Now this code is compiling and giving me an output of
Hi from show6 Child
Exception in thread "main" java.lang.ArithmeticException
Press any key to continue . . .
What do you say?
Then I added these 2 lines
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
Process completed with exit code 1
Can you explain.
Its importatnt as I'm preparing for SCJP.
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.