• 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

Generics

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

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() { ... }

In this pgm ,I thought answers are b,c,d,e.But The Answer page shows only b,d,e.
Option c is overloaded version of set(Number e) like option d.Isn't it?Can anybody clarify this answer?Thank you.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Parent class is holding a Number.



So you should put a Number into the parent class.

An Object isn't a Number. So you can't put an Object in your class. A Long and a Number are Numbers. So these are valid.

I hope this helps,
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The answer is type erasure.

Generic typing is only compile time, so at runtime the signature of the set(..) method of Parent<Integer> looks like set(Object e).

Therefore if you try to compile with option c. you'll get something like

Name clash: The method set(Object) of type Child has the same erasure as set(E) of type Parent<E> but does not override it
 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Name clash: The method set(Object) of type Child has the same erasure as set(E) of type Parent<E> but does not override it



NOt getting .can anyone elaborate this???
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi raj,

behind the scenes, the compiler generates something like the following for your parent class.

As result, if you do something like this
you don't overload the method, you override it. And this is not allowed.


For more details have a look at Type Erasure in the Java Generics FAQ
 
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 ranchers,

here we have two name clashes.

Name Clash One, example from Kathir, abridged version:


This does not compile. See from the others name clash.
But what if it did?
By extending the parent class this way, you have already inherited the method set(Number n).
So our method set(Object o) looks like a legal overload.
But, as we've already seen, the parent class has also this method by type erasure. So it is also an override.
Overload and override in the same place?
Could this be allowed?

Discussion open.


Name Clash Two:
Dear "Lighted Jones", you may not yet have noticed, that we have a policy regarding user names on the ranch. Please read here why:
http://www.javaranch.com/name.jsp


So, would you please change your user name before your next posting?
It will not affect anything you've already posted here. Just your user name will update.




Yours,
Bu.
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic