This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interface question Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Interface question" Watch "Interface question" New topic
Author

Interface question

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486


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.
:?:



Thanks<br />Dinesh
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
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.
:?:


Sunny Mattas
Ranch Hand

Joined: Apr 22, 2008
Posts: 45
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


Regards
Sunny Mattas
SCJP5
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
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

Joined: Sep 04, 2007
Posts: 85
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.


Thanks
Sunil (SCJP 5)
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Thanks for simple exception

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

Please tell me
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
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

Joined: Sep 04, 2007
Posts: 85
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.
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12911
    
    3

Please use code tags when you post source code.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Interface question
 
Similar Threads
GarbageCollector
Behaviour of return() ???
Casting subclass to superclass type
servlet cant see getServletContext() from inheritance
Interface