Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generic Ques

 
Mohammed Yousuff
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the Difference between Refereing a Vector like Vector<?> or with Vector<Object>

For Example

import java.util.*;


class Test {

public static void main(String args[]) {

Vector<?> vec = new Vector<A>();

vec = new Vector<B>();
}
}

abstract class timepass{

}

class A extends timepass{

}

class B extends timepass {

}
 
wise owen
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generics in the Java Programming Language Page 5. may help you.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of ? any class could be in there. In the case of Object, only Objects of class Object can be placed in there, you cannot put in any subclasses. So that means you would have to downcast all your subclasses to type Object. Very simplified version, and you should study it more, but for simply determining what goes in the Vector<?> vs Vector<Object>

Mark
 
Amirr Rafique
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys
As far as my idea i concern in case of Vector<?> we can replace ? with any thing i.e. Object or Interface
[/list]
But for Vector<Object> you can not pass Interface as type like
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic