aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Help understanding a question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Help understanding a question" Watch "Help understanding a question" New topic
Author

Help understanding a question

Bren Reg
Greenhorn

Joined: Feb 20, 2012
Posts: 7
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

Joined: Sep 20, 2010
Posts: 3615
    
  14

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

Joined: Feb 20, 2012
Posts: 7
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

Joined: Mar 10, 2008
Posts: 1761
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3615
    
  14

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

Joined: Feb 20, 2012
Posts: 7
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!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help understanding a question