Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help understanding a question

 
Bren Reg
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey new to forum. Just need help understanding a question in a SCJP book im reading atm

Question is..

13. Given the following class definitions




what is the result of the following statement?

new Child().printResults(0);

A. In Parent
B. In Child
C. 0
D. Line 2 generates a compiler error.
E. Line 8 generates a compiler error.

I said answer=E as the child method printResults is overridding the parent one and you cannot change the method signature but this is the answer the book has given..

13. B. The code compiles fine, so D and E are incorrect. The printResults method in Child is overloading printResults in Parent, not overriding. In method overloading, the return type can be any data type, so printResults in Child returning an int is not a problem.
Invoking printResults with an int argument calls the method on line 8, which displays In Child. Therefore, the answer is B.

How can I tell that that is overloading and not overriding??

Thanks for the help
 
Stephan van Hulst
Bartender
Pie
Posts: 5553
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By seeing that the method has the same name, but a different number of parameters, or different parameter types.

Child inherits printResults(String...) from the Parent. But it also declares printResults(int). Method overloading is just a fancy name for when two different (and possibly unrelated) methods happen to share the same name. It's perfectly legal, but as you just found out, it can be confusing.
 
Bren Reg
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:By seeing that the method has the same name, but a different number of parameters, or different parameter types.

Child inherits printResults(String...) from the Parent. But it also declares printResults(int). Method overloading is just a fancy name for when two different (and possibly unrelated) methods happen to share the same name. It's perfectly legal, but as you just found out, it can be confusing.


Hey Stephan, thanks for the reply. So would I be right in sayin that in a subclass you would never have compiler errors resulting from overriding rules being broken as the method will just be overloaded instead?
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unless you specify a more restrictive access modifier, a new or broader Exception type in the throws clause, or specify a return type that is not covariant.
So, still plenty of ways to upset the ol' compiler.
 
Stephan van Hulst
Bartender
Pie
Posts: 5553
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pretty much. If you declare a method with different parameter types than one declared in the super-class, it will be overloaded instead.

The only thing you can't do is overload a method using a generic type parameter that has the same signature after type erasure. For instance, if the parent method took a Number, you weren't allowed to give Child the following method declaration:
 
Bren Reg
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys. I think I get it now. I understand overloading and overriding ok but it's the first time I've come across an overloaded method in a child class. It threw me off a bit! Thanks for the help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic