• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Override and overload of generic method

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given



Which two statements are true if the method is added to Bar? (Choose two.)
A. public Collection<String> foo(Collection<String> arg) { ... } overrides Foo.foo.
B. public <T> Collection<T> foo(Stream<T> arg) { ... } overloads Foo.foo.
C. public <T> List<T> foo(Collection<T> arg) { ... } overrides Foo.foo.
D. public <T> Collection<T> foo(Collection<T> arg) { ... } overloads Foo.foo.
E. public <T> Collection<T> bar(Collection<T> arg) { ... } overloads Foo.foo.
F. public <T> Iterable<T> foo(Collection<T> arg) { ... } overrides Foo.foo.

The answer in the key is CF but I think A and B are correct too, what do you guys think?
Screen-Shot-2022-05-25-at-11.34.04.png
[Thumbnail for Screen-Shot-2022-05-25-at-11.34.04.png]
 
author & internet detective
Posts: 41124
848
Eclipse IDE VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Du,
Welcome to CodeRanch!

A and B are not correct. A is not a valid override because only the generic is different. Due to type erasure, you can't have two methods that would have the same signature if generics were gone. B is not a valid override because Stream/Collection are not in the same class hierarchy.
 
Du Tran
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeanne Boyarsky wrote:
B is not a valid override because Stream/Collection are not in the same class hierarchy.



Thanks Jeanne, super excited to have the author of the OCP book answered my question

Actually B is considered a overloaded method. Return type does not matter, method name is the same. Input type is different as they are not in the same hierarchy. So what makes it an invalid overloaded method?
 
Bartender
Posts: 4981
186
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Du,

You are right, overloading in answer B is correct. I just tried:

outcome:
in Foo
in Bar


Edit: I just noticed that Jeanne, in her reply, mentions that B is not a correct override (which is correct). But the exercise mentions overload in B.
 
Du Tran
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Piet, clear example
 
Jeanne Boyarsky
author & internet detective
Posts: 41124
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah. I read it to fast and thought both were about overriding!
 
Jeanne Boyarsky
author & internet detective
Posts: 41124
848
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you let me know which book/chapter that is from so I can update the errata?
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic