• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

doubt regarding overloading

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which of them are valid inserts?
class Parent<E>
{
public E get() { return null;}
public void set(E e) { }
}
class Child extends Parent<Number>
{
//insert here
}

options given to insert are

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


and ans is b,d,e.Iam fine with it.

But i have confusion with option c.Why "option c" is wrong?In option c,
we hava "Object arg" as argument, so isn't it a valid overloading example?
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The C option is incorrect.
All the information about generics is for compile time only, it's erased for the runtime. The method signature that's left is called erasure.
For example, for the method set(E e) in the Parent class, the erasure's going to be set(Object e).
So during runtime you're gonna have two identical methods, which were not explicitly overriden: public void set(Object e) in the Parent class and public void set(Object arg) in the Child class. This causes the name clash.
[ December 21, 2007: Message edited by: Serge Petunin ]
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maan Shenoy, please quote your sources.
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Serge Petunin:

All the information about generics is for compile time only, it's erased for the runtime. The method signature that's left is called erasure.
For example, for the method set(E e) in the Parent class, the erasure's going to be set(Object e).
So during runtime you're gonna have two identical methods, which were not explicitly overriden: public void set(Object e) in the Parent class and public void set(Object arg) in the Child class. This causes the name clash.

[ December 21, 2007: Message edited by: Serge Petunin ]



Above i got it from mock exam www.javabeat.com site.
But i am still not satisfied ...see here their is a statement "class child extends Parent<Number>
so parent class set(E e) should be (Number e)
and in child class if we have option c, which is set(Object e), then what is wrong in it?Wont it be a vaild overloading example.

Iam bit confusede here.....

[ December 22, 2007: Message edited by: Maan Shenoy ]
[ December 23, 2007: Message edited by: Maan Shenoy ]
 
author
Posts: 23956
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But i am still not satisfied ...see here their is a statement "class child extends Parent<Number>
so parent class set(E e) should be (Number e)
and in child class if we have option c, which is set(Object e), then what is wrong in it?Wont it be a vaild overloading example.



The parent class has to work for all types of E. There isn't a different class file for each instance which it is used.

Henry
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic