• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem with generic type and subtype  RSS feed

 
Jahangir Alam
Greenhorn
Posts: 15
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can I add a subclass object of Employee type in above mentioned ArrayList ?



What type of object I can add here? Only the subclass object of Employee or both Employee object and its subclass objects?

I studied invarience and covarience. But I am still confusing.

Any explanation would be great for me!

 
Muhammad Khojaye
Ranch Hand
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jahangir Alam wrote:Can I add a subclass object of Employee type in above mentioned ArrayList ?

What happen when you try to insert it?

Jahangir Alam wrote:
What type of object I can add here? Only the subclass object of Employee or both Employee object and its subclass objects?

"? extends Employee" will allow you to add both collection of type Employee and also of subtype of Employee. I again encourage you to write a simple program and test it yourself .
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been through the Java Tutorials?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!