| Author |
Method Exception declaration when inheritance.
|
jamil lusa
Ranch Hand
Joined: Aug 18, 2011
Posts: 58
|
|
hi all i have seen this mock test question and i am confused :
the answer given is like this:
Both classes do not override the toString method appropriately.
However they override it according to the rules based on which overriding should be done.
The output of this program would be something like Fir@1368a
But i think the relationship between NullPointerException and RuntimeException will make the program cannot be compiled. So is my thinking right?
thanks.
|
 |
Will Myers
Ranch Hand
Joined: Aug 05, 2009
Posts: 285
|
|
A NullPointerException is a RuntimeException so the method in Fir can throw any supertype of NullPointerException.
have a read of this: Rules
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3795
|
|
And remember that they are unchecked exceptions, so don't need to be declared anyway.
By the way - the easiest and most reliable way to find out if something will compile is to try and compile it. We'll sometimes make a mistake. The compiler won't .
|
 |
jamil lusa
Ranch Hand
Joined: Aug 18, 2011
Posts: 58
|
|
Let me conclude my thought, see whether my study is right or not.
1) if parent is checked exception, child can be having a) same or narrower exception b) no exception c) ANY unchecked exception.
2) if parent is unchecked exception, child can be having a) ANY unchecked exception b) no exception
3) if parent no exception, child can be having a) ANY unchecked exception b) no exception
anyway, are the rules that i stated above correct?
Thanks.
|
 |
jamil lusa
Ranch Hand
Joined: Aug 18, 2011
Posts: 58
|
|
Anyone can confirm my answer??
|
 |
Ivan Amat
Greenhorn
Joined: Jul 26, 2010
Posts: 4
|
|
Hi Jamil,
I would add a little nuance to the point 1)
1) if parent is checked exception, child can be having a) same or narrower exception or fewer exception b) no exception c) ANY unchecked exception.
2) if parent is unchecked exception, child can be having a) ANY unchecked exception b) no exception
3) if parent no exception, child can be having a) ANY unchecked exception b) no exception
|
 |
Yui Huang
Greenhorn
Joined: Apr 02, 2011
Posts: 7
|
|
jamil lusa wrote:
the answer given is like this:
Both classes do not override the toString method appropriately.
However they override it according to the rules based on which overriding should be done.
The output of this program would be something like Fir@1368a
I was tricked by the question so I checked the API. To override the toString method, it should be like this:
which should be "public" and takes "no arg". Thanks! I learned one more thing today!
|
 |
Janki Shah
Ranch Hand
Joined: Nov 23, 2011
Posts: 136
|
|
Is this really an override of the String method (public String toString())? I don't think it is. Is it an overloaded method of the String method or it's new method?
It should print "Fri" why this code is printing "Fri@1232097409" ?
Please, can any one explain this?
|
 |
Janki Shah
Ranch Hand
Joined: Nov 23, 2011
Posts: 136
|
|
|
Can someone explain my question posted above , please?
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Janki Shah wrote:Is this really an override of the String method (public String toString())? I don't think it is. Is it an overloaded method of the String method or it's new method?
It should print "Fri" why this code is printing "Fri@1232097409" ?
Please, can any one explain this?
Can you tell us why you think the output should be "Fri"?
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Janki Shah
Ranch Hand
Joined: Nov 23, 2011
Posts: 136
|
|
Thank you Henry for your reply.
why reply should be Fir?. because I think it's a new method in the Test class (not the same method declared in Object class because the argument and the return type). just happens to have the same name.
and Fir class follows all the overriding rules.
But I am still confuse about one thing that is it really a new method or the overloaded method of Object class's method toString().
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1778
|
|
It's an overloaded toString() method in Test class (overloaded on the Object's toString()) and an overridden toString() method in Fir class (overriding Test's toString() method).
When an object is printed using System.out.println() the toString() method which takes no argument is called, and hence you see Fir@<some hexa value>.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16695
|
|
Janki Shah wrote:Thank you Henry for your reply.
why reply should be Fir?. because I think it's a new method in the Test class (not the same method declared in Object class because the argument and the return type). just happens to have the same name.
and Fir class follows all the overriding rules.
But I am still confuse about one thing that is it really a new method or the overloaded method of Object class's method toString().
I am a bit confused, as you seem to be contradicting yourself.
On one hand, you say that it is overloading -- that it is a not the same toString() method defined in the Object class. This part, you are correct. On the other hand, you are surprised that the toString() method of the object class is called. Of course it is called. The println() uses the toString() method defined in the Object class, and since the Fir class didn't override the Object toString() method, why should it be called instead?
Henry
|
 |
Janki Shah
Ranch Hand
Joined: Nov 23, 2011
Posts: 136
|
|
I am very sorry for the confusing words.
Let me clarify,
1. I don't understand "Is it an overloading or overriding or a new method declared in Test class and then that new method is overriden in Fir class?"
|
 |
Janki Shah
Ranch Hand
Joined: Nov 23, 2011
Posts: 136
|
|
Hi Henry and John,
I tried compiling this after you explanation
|
 |
 |
|
|
subject: Method Exception declaration when inheritance.
|
|
|