Java Overriding and Overloading throw errors at compile time?
I have been coding few examples on method overloading and method overridng. Method overloading is static polymorphism and overriding is dynamic polymorphism.
So any error related to overloading will be caught at the compile time..Correct ?or are there any scenarios when a runtime exception is thrown because of incorrect overloading -- I Doubt
Any errors related to method overriding( -- incase when the child class method has same name as Base class and same arguments as of base class ---- )will be thrown at compile time .
Are th开发者_运维百科ere any scenarios in both the above cases where there is a exception at run time ?
So any error related to overloading will be caught at the compile time..Correct ?or are there any scenarios when a runtime exception is thrown because of incorrect overloading -- I Doubt
No it shouldn't if your code from the method doesn't throw any runtime exception.
Any errors related to method overriding( -- incase when the child class method has same name as Base class and same arguments as of base class ---- )will be thrown at compile time .
This is what is called overriding. if its not there then it may throw compile time error if you use @Override
I cannot imagine how method overloading can cause a runtime exception.
Some scenarios when incorrect/incomplete method overriding can cause runtime exceptions:
- Base method is abstract, while it was not implemented by the child class - this may happen, when the parent class was recompiled adding new method, while the child class was not recompiled (buggy lazy compilation, or classes located in different jars, etc)
- Overriden method is called from the constructor of a base class.
- Related to 1: the base method is not abstract, but the signature was changed, while the child class was not recompiled. So it didn't caused the compilation error and will fly in runtime.
精彩评论