File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes polymorphism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "polymorphism" Watch "polymorphism" New topic
Author

polymorphism

Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Hi Ranchers...


please clear me one thing.........
in case of polymorphism..
if the superclass contains a method that is declared to throw a checked exception.......

then the subclass method should always be declared to throws the same exception or the subclass of exception...............


this is correct..........

but i want to know whether this applies to unchecked(or Runtime) exception also............

please clearify.........


SCJP6.0,My blog Ranchers from Delhi
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

why don't you give a try first your self?


SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

shanky sohar wrote:Hi Ranchers...


please clear me one thing.........
in case of polymorphism..
if the superclass contains a method that is declared to throw a checked exception.......

then the subclass method should always be declared to throws the same exception or the subclass of exception...............


even this is not correct
and there is really simple solution over this
why don't you write a simple program
and if there is difficulty , experts here are always ready to help

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Harpreet Singh janda
Ranch Hand

Joined: Jan 14, 2010
Posts: 317

Not always but sub class method canthrow the same exception or subclass of the exception.
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Minhaj Mehmood wrote:why don't you give a try first your self?



how can i ask such a simple question....


Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

i just tried it..........
it doesnot seems to be like that.......only in case of checked exception....that happen........

see the below code.........


Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

but confirm me this thing whether this happen always or in some cases it doesnot work........
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9307
    
  17

in case of polymorphism..
if the superclass contains a method that is declared to throw a checked exception.......

then the subclass method should always be declared to throws the same exception or the subclass of exception...............

this is correct..........

This is not right. If the super-class method throws some exceptions, the sub-class method can throw those exceptions or may not throw any exception...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Prasad Kharkar wrote:
even this is not correct
and there is really simple solution over this
why don't you write a simple program
and if there is difficulty , experts here are always ready to help


Hi.....Prasad.......see the question is not that much simple as we all think.........
i really got confused many times......because of this.......
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10240
    
168

shanky sohar wrote:
Hi.....Prasad.......see the question is not that much simple as we all think.........
i really got confused many times......because of this.......


Just a slightly off topic suggestion - please do not use those ........ unnecessarily. It really makes it difficult to read your posts.

[My Blog] [JavaRanch Journal]
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

okay
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Hi, shanky sohar,

What do you get, if you've changed the program as below?


Could you able get the different between them?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

@shanky
yes it is
the question is simple and no complications in it at all
I think you should study the overriding rules thoroughly
you say that the overriding method should throw same exception or subclass of the exception
but this is not true always
you can override with the method that throws no exception at all
why don't you try a program?
that will really make sense
just write a method that throws checked exception or unchecked exception
and override the method with a method that does not throw any exception
that should solve the problem
just give it a try
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Prasad Kharkar wrote:@shanky
yes it is
the question is simple and no complications in it at all
I think you should study the overriding rules thoroughly
you say that the overriding method should throw same exception or subclass of the exception
but this is not true always
you can override with the method that throws no exception at all
why don't you try a program?
that will really make sense
just write a method that throws checked exception or unchecked exception
and override the method with a method that does not throw any exception
that should solve the problem
just give it a try


Yes i tried it.........
Avishkar Nikale
Ranch Hand

Joined: Aug 06, 2010
Posts: 173
Hi Shanky,

Once you have tried the overriding test code try to think in following terms...

Try to remember what the "throws" explains about checked exception as a contract about the class.
The class method has a risky behavior which might fail.

It might or might not throw the given checked exception, but caller should be aware.

So the leniency for the subclass is due to the same fact that it might be a safer method which
i) does not throw any exceptions
ii) does not throw any broader exeception (since it has to oblige by the parent class contract)
but throws a narrower subclass exception

A subclass can emilinate the risky code or narrow it down but at no point broaden it.

As for the runtime exceptions part they can be
i) declared , not declared
ii) narrowed
iii) broadended

Regards,
Avishkar Nikale
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: polymorphism