• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Exception declaration when inheritance.

 
jamil lusa
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 383
Java jQuery Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone can confirm my answer??

 
Ivan Amat
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone explain my question posted above , please?
 
Henry Wong
author
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Janki Shah
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21427
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Henry and John,
I tried compiling this after you explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic