• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help understanding a question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15524
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic