Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

generic code

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


Above code compiles without warning why?
can any one explain?
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vikram,
Actually your confusion runs around last couple of lines of
your code;


class NewClass{
public static void main(String[] args) {

LinkList<String> s=new LinkList<String>();
LinkList<Object> list1;
LinkList<String> list2;
//list1=list2; // this line generates compilation error incompatible types

}
}



Parameterized (generic) assignment doesn't happen in that way.
In one line,
"polymorphism applies to base type not parameterized type."

LinkedList<String> list1 = new ListedList<String>();

Here LinkedList is the base type and <String> is parameterized type.
You can apply the quoted line with the following line:

List<String> list2 new LinkedList<String>();
Here List and LinkedList are base types so no problem. Polymorphism
(parent class/interface type ref variable holding object of subtype/implementing class type object) applies to base type;

You can't write like:
List<Object> list3 = new LinkedList<String>(); //Line #1

under the impression that String IS-A(n) Object. That would be wrong.
Actually these type informations are erased finally. They are only to restrict the programmers so that nothing wrong can be inserted to the List.

After type erasure the Line #1 would become
List list3 = new LinkedList();
[ April 29, 2007: Message edited by: Chandra Bhatt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Above code compiles without warning why?



Sorry, I cannot read your mind. What warnings are you expecting and on what lines?
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic