• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

mock question

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Parent<E>
{
public E get() { return null;}
public void set(E e) { }
}
class Child extends Parent<Number>
{
//insert here
}
which are all valid method(s) inserted independently will allow program to compile

a public Object get() { ... }
b public Long get() { ... }
c public void set(Object arg) { ... }
d public void set(Long arg) { ... }
e public Number get() { ... }



Answer : b,d,e
why c is not allowed
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because Object can only be a super class of <E>, being the superclass of any java object. Hence, overriding methods can only have parameters that pass the IS-A test with the superclass parameters. In that case, Object doesn't pass the test, whatever is E.

PS: I'll bet someone will soon ask you to change your display name for your real name !
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Name clash.

See this thread:
https://coderanch.com/t/263763/java-programmer-SCJP/certification/Generics


Yours,
Bu.
 
Dave Walsh
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Burkhard Hassel:
Name clash.

See this thread:
https://coderanch.com/t/263763/java-programmer-SCJP/certification/Generics


Yours,
Bu.



Are you serious ? It's not an overriding method IS-A parameter problem ?
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy!

The problem comes from type erasure. The compiler says something like "name clash, the method has the same parameter like erasure type but does not override it."

Method set(Object o) looks like a legal overload of method set(Number n) in class Child. Child however does not have the object- method. You cannot say
new Child().set (new Object());

but because the Child class inherits the erasure type set(Object) you cannot use nor overload nor override it.



Yours,
Bu.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy again,

why it has nothing to do that Object is higher in hierchy than Number cannot be demonstrated in the original code, since Number directly extends Object.
However compare this situation:


set(A a) works despite of the fact that the class extends Parent<B> and A is higher than B. It is just an overload.
Only with set(Object x) this is neiter overload nor override but a (non-compiling) name crash.


Yours,
Bu.
 
Don't play dumb with me! But you can try this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic