• 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
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Interface question

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


output : hi

Hello ranchers,
The above code complies and gives result as shown in output
How is this possible? interface is defining the method which has checked exception and when i am implementing in my class the same method but with different exception which is unchecked exception how its showing no error. Can you please tell what is happening here?
Whether i am overidding the method or overloading the method.
:?:


 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The overriding method can throw NO Exception, No new Checked Exception, Subclass of checked exception declared in overidden function and NEW UNCHECKED EXCEPTION.

In your case NullPointerException is an Unchecked Exception.

Hope it clarifies

And please use CODE tag in order to wrap your code examples for better visibility. Its a rule on the forum.

Cheers !!!




Dinesh Tahiliani wrote:

output : hi

Hello ranchers,
The above code complies and gives result as shown in output
How is this possible? interface is defining the method which has checked exception and when i am implementing in my class the same method but with different exception which is unchecked exception how its showing no error. Can you please tell what is happening here?
Whether i am overidding the method or overloading the method.
:?:


 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These line from k&b chapter 2 page 102 will explain

The overriding method must NOT throw checked exceptions that are new
or broader than those declared by the overridden method. For example, a
method that declares a FileNotFoundException cannot be overridden by a
method that declares a SQLException, Exception, or any other non-runtime
exception unless it's a subclass of FileNotFoundException.


The overriding method can throw narrower or fewer exceptions. Just because
an overridden method "takes risks" doesn't mean that the overriding subclass'
exception takes the same risks. Bottom line: an overriding method doesn't
have to declare any exceptions that it will never throw, regardless of what the
overridden method declares.



:?
Regards
Sunny
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't get you can you explain in simple terms please.
Wht about then overrloaded methods? Can they throw new/broader checked or unchecked exception. Please anyone eplain me with example. Iam totally confused between these 2 things :?:
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Dinesh, Please check the rule for Overriding, Overriding method may throw narrower checked and any type of unchecked exception that is what happening in your code..Hope it clear you and please use code tags for further discussions...All the best.
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks for simple exception

Then what about overloaded methods, can they throw narrower or unchecked exceptions?

Please tell me

 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember the overriding rules i mentioned above.

For overloading there is no restriction. And it doesn't depend on return type. Just the type and number of arguments have to be different. When its neither overloading , nor overriding then it will be a error.

Try solving few questions with above rules in mind and no question can trick you.
All the best.


Dinesh Tahiliani wrote:Didn't get you can you explain in simple terms please.
Wht about then overrloaded methods? Can they throw new/broader checked or unchecked exception. Please anyone eplain me with example. Iam totally confused between these 2 things :?:

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

Dinesh Tahiliani wrote:

Thanks for simple exception

Then what about overloaded methods, can they throw narrower or unchecked exceptions?

Please tell me



Yes, Overloaded methods may throw different Exceptions.
 
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:
  • Quote
  • Report post to moderator
Please use code tags when you post source code.
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic