Generic Class Problem

Consider the following simple class
public myClass<E,F> {
   \\ Constructor
   public myClass(Object oE, Object oF) {
      \\ constructor code
}Does anyone know how to impose at compile time that Object oE implements an Iterator iterface over a colection of type E and Object oF implements an Iterator iterface over a colection of type F?
If not what is the best way of doing this at run time?

I am not sure I understand. If I only have the
Iterator interface passed into the constructor, how
would I access some of the other methods on the
object? I would not know which class to cast it back
to.If you need to access other methods, then obviously "has to implement Iterator<E>" is by far not the only constraint the object has to fulfill, hence your interface is underspecified.
You said "all you care about is that it's an Iterator<E>". I gave you that. If you need something else, you should say that.

Similar Messages

  • Generic classes with parameterized super class and interface

    Hello.
    I'm trying to write a generic class that looks like the following pseudo-code.
    public interface Interface {
        public Interface getSibling(...);
    public class Klass {...}
    public class MyKlass<T extends Klass, T2 extends Interface>
            extends T implements T2 {
        public T2 getSibling(...) {
            return this;
    }where Interface and Klass each have various extensions.
    I came across this problem, or challenge, while writing classes for testing my EJBs. I tried and failed various attempts.
    Is it possible to write generic classes of this nature in Java?
    If so, please tell me and others who are like me.
    Thanks in advance.

    No. That would not work.
    Beside being forbidden by the compiler, to my understanding, it cannot be done in theory either, as the parameterized types get bound at instantiation time and are not available for static reference, which both extends and implements require.

  • Support for generic classes

    hi,
    i m using creator 2 update 1, it does not support generic classes.
    it says use source 1.5 for generic classes.
    please tell me how to solve this problem
    thank you

    Well, not necessarily true... Most of the latest graphics cards work off of a common core, which is named depending on its maximum tested performance and on how many pipelines passed quality checks.
    Thus a 7300 is fundementally identical to a 7800, except that it uses fewer fragment/vertex pipelines and is clocked lower. This is done either artificially by disabling parts of the GPU, or if when they fail quality control.
    Either way, the drivers are the same, and have been unified for a long time for the entire product line. In fact if you look at the drivers for the card in yoru computer you will see they are more then likely for a different chipset. My MBP claims the drivers are for a X1000, even though the card is a X1600.

  • Need help with generic class with comparable type

    Hi. I'm at University, and I have some coursework to do on writing a generic class which offers ordered binary trees of items which implement the comparable interface.
    I cant get the code to compile which I have written.
    I get the error: OBTComparable.java uses unchecked or unsafe operations
    this is the more detailed information of the error when I compile with -Xlint:unchecked
    OBTComparable.java:62: warning: [unchecked] unchecked call to insert(OBTType) as
    a member of the raw type OBTComparable
    left.insert(insertValue);
    ^
    OBTComparable.java:64: warning: [unchecked] unchecked call to insert(OBTType) as
    a member of the raw type OBTComparable
    right.insert(insertValue);
    ^
    OBTComparable.java:75: warning: [unchecked] unchecked call to find(OBTType) as a
    member of the raw type OBTComparable
    return left.find(findValue);
    ^
    OBTComparable.java:77: warning: [unchecked] unchecked call to find(OBTType) as a
    member of the raw type OBTComparable
    return right.find(findValue);
    ^
    and here is my code for the class
    public class OBTComparable<OBTType extends Comparable<OBTType>>
      // A tree is either empty or not
      private boolean empty;
      // If the tree is not empty then it has
      // a value, a left and a right.
      // These are not used it empty == true
      private OBTType value;
      private OBTComparable left;
      private OBTComparable right;
      // Create an empty tree.
      public OBTComparable()
        setEmpty();
      } // OBTComparable
      // Make this tree into an empty tree.
      private void setEmpty()
        empty = true;
        value = null; // arbitrary
        left = null;
        right = null;
      } // setEmpty
      // See if this is an empty (Sub)tree.
      public boolean isEmpty()
      { return empty; }
      // Get the value which is here.
      public OBTType getValue()
      { return value; }
      // Get the left sub-tree.
      public OBTComparable getLeft()
      { return left; }
      // Get the right sub-tree.
      public OBTComparable getRight()
      { return right; }
      // Store a value at this position in the tree.
      private void setValue(OBTType requiredValue)
        if (empty)
          empty = false;
          left = new OBTComparable<OBTType>(); // Makes a new empty tree.
          right = new OBTComparable<OBTType>(); // Makes a new empty tree.
        } // if
        value = requiredValue;
      } // setValue
      // Insert a value, allowing multiple instances.
      public void insert(OBTType insertValue)
        if (empty)
          setValue(insertValue);
        else if (insertValue.compareTo(value) < 0)
          left.insert(insertValue);
        else
          right.insert(insertValue);
      } // insert
      // Find a value
      public boolean find(OBTType findValue)
        if (empty)
          return false;
        else if (findValue.equals(value))
          return true;
        else if (findValue.compareTo(value) < 0)
          return left.find(findValue);
        else
          return right.find(findValue);
      } // find
    } // OBTComparableI am unsure how to check the types of OBTType I am comparing, I know this is the error. It is the insert method and the find method that are causing it not to compile, as they require comparing one value to another. How to I put the check in the program to see if these two are of the same type so they can be compared?
    If anyone can help me with my problem that would be great!
    Sorry for the long post, I just wanted to put in all the information I know to make it easier for people to answer.
    Thanks in advance
    David

    I have good news and undecided news.
    First the good news. Your code has compiled. Those are warnings not errors. A warning is the compiler's way of saying "I understand what you are asking but maybe you didn't fully think through the consequences and I just thought I would let you know that...[something] "
    In this case it's warning you that you aren't using generics. But like I said this isn't stopping it from compiling.
    The undecided news is the complier is warning you about not using generics. Are you supposed to use generics for this assignment. My gut says no and if that's true then you have no problem. If you are supposed to use generics well then you have some more work.

  • Generic Class Errors continue

    Hi have had "generic class" errors with my printers on my MacPro for over two years now. First with a Samsung and now with a Brother printer. I have the latest drivers for the Mac, I have the latest Leopard and I still get these really annoying errors that require restarting everything. I had the problems with Tiger too.
    Is there any way to avoid this kind of mess?
    Any help appreciated.

    kittireddy wrote:
    What is "Generic class" in Java? and what's use of that?It's just added to confuse new programmers.
    Give me an idea with an exampleNo problem:
    public class GenericsExample {
        public static void main(String[] args) {
            new X<String>(new X<String>("Y"));
    class X<Y> {
        Y x;
        X(Y y) { x = y; }
        X(X<Y> y) { System.out.println("y.x = "+y.x); }
    }

  • Using static .values() method of Enum in Generic Class

    Hi *,
    I tried to do the following:
    public class AClass<E extends Enum<E> >  {
         public AClass() {
              E[] values = E.values(); // this DOESN'T work
              for (E e : values) { /* do something */ }
    }This is not possible. But how can I access all Enum constants if I use
    an Enum type parameter in a Generic class?
    Thanks for your help ;-) Stephan

    Here's a possible workaround. The generic class isn't adding much in this case; I originally wrote it as a static method that you simply passed the class to:
    public class Test21
      public static enum TestEnum { A, B, C };
      public static class AClass<E extends Enum<E>>
        private Class<E> clazz;
        public AClass(Class<E> _clazz)
        {  clazz = _clazz;  }
        public Class<E> getClazz()
        {  return clazz;  }
        public void printConstants()
          for (E e : clazz.getEnumConstants())
            System.out.println(e.toString());
      public static void main(String[] argv)
        AClass<TestEnum> a = new AClass<TestEnum>(TestEnum.class);
        a.printConstants();
    }

  • Hp LaserJet 1200 Generic Class: Waiting for device

    I have 2 computers that I want to print to my hp Laserjet 1200 printer. We are using an iogear 4-port usb 2.0 sharing switch. One computer prints fine but the other always displays a message "Generic Class: Waiting for device." Help!

    Both computers have the printer driver installed. The usb device does not have a driver to install. You just plug in the usb cords to each computer and the printer. You only install a driver if using a pc operating system.

  • Best way to call a function in a generic class from a base class

    Hi,
    I have a generic class that works with subclasses of some other baseclass. When I construct the subclass, I call the constructor of the baseclass which I want to call a function in the generic class. e.g. suppose I have the following classes
    public class List<T extends BaseClass>
        newTCreated(T t)
    }

    Sorry, I pressed Tab and Enter last time when typing the code so I posted without meaning to
    Hi,
    I have a generic class that works with subclasses of some other baseclass. When I construct the subclass, I call the constructor of the baseclass which I want to call a function in the generic class. e.g. suppose I have the following classes
    public class List<T extends BaseClass>
        public void newTCreated(T t)
            // add the t to some internal list
        public T getT(int index)
            // get the object from the internal list
    public class BaseClass
        public BaseClass(List<?> list)
            list.newTCreated(this);
    public class SubClass extends BaseClass
        public SubClass(List<SubCass> list)
            super(list);
    }This doesn't compile because of the call to newTCreated in the BaseClass constructor because BaseClass is not necessarily of type T. Is there any way of checking when I call the newTCreated function that the BaseClass is actually of type SubClass? I could either add the call explicitly in each SubClass's constructor or have a function addToList in BaseClass that is called from the BaseClass constructor but overloaded in each subclass but both of those rely on future subclasses doing the same. Or I could change the newTCreated function to take an argument of type BaseClass and then cast it to type T but this doesn't give a compilation error, only a runtime exception.
    It seems like there should be solution but having only recently started writing Generic classes I can't find it. Thanks in advance for any help,
    Tom

  • Getting the name of the generic class?

    I have a generic class declared as such:
    public class MyClass<T>
    and I want to access the name of the <T> class a member function in that class? I just can't figure out which syntax to use.
    Thank you!
    Joshua

    Sorry, I'm a little bit confused at your answer.
    I cannot seem to instantiate an object of class T, like: T item = new T();does not work. If I can't create an object of type T, then I can't get it's type. I need to get the class even if I don't have any instantiated objects (or if I can create a blank one), so that I can use the name to fetch the objects from the persistence engine. Here is the class, so you can see what I'm talking about. I have in there now what seems like it should be right, but doesn't compile.
    A small example of solution please?
    public class PersistentSelectorCellEditor<T> extends AbstractCellEditor
              implements TableCellEditor {
         JComboBox control;
         List<T> choices;
         Session session;
         public PersistentSelectorCellEditor() {
              initializeComponents();
         public PersistentSelectorCellEditor(Object value) {
              initializeComponents();
              this.control.setSelectedItem(value);
         private void initializePersistence() {
              session = LabApp.getSession();
              session.beginTransaction();
         private void initializeComponents() {
              initializePersistence();
              populateList();     //query for selections to fill combo box
              control = new JComboBox((ComboBoxModel)choices);
         private void populateList() {
              //query to populate drop down lists
              Query query = session.createQuery("from " + T.class.getName());
              choices = query.list();
         public void setValue(T value) {
              control.setSelectedItem(value);
         public T getValue() {
              return (T)control.getSelectedItem();
         //interface members
         public Object getCellEditorValue() {
              return control.getSelectedItem();
         public Component getTableCellEditorComponent(JTable table, Object value,
                   boolean isSelected, int row, int column) {
              return control;
    }Thank you!
    Joshua

  • Array of generic class

    Hi.
    i have to create an array of generic class
    ex: GenericClass<E> [ ] x = new GenericClass<E>[ y ]
    is it possible?
    i can create GenericClass<E> [ ] x; but i can't initiate it in this way..
    Someone know how i can do it?

    crosspost
    http://forum.java.sun.com/thread.jspa?threadID=746524&messageID=4272614#4272614

  • What is the use of Generic class in java

    hi everyone,
    i want to know that
    what is the use of Generic class in java ?
    regards,
    dhruvang

    Simplistically...
    A method is a block of code that makes some Objects in the block of code abstract (those abstract Objects are the parameters of the method). This allows us to reuse the method passing in different Objects (arguments) each time.
    In a similar way, Generics allows us to take a Class and make some of the types in the class abstract. (These types are the type parameters of the class). This allows us to reuse the class, passing in different types each time we use it.
    We write type parameters (when we declare) and type arguments (when we use) inside < >.
    For example the List class has a Type Parameter which makes the type of the things in the list become abstract.
    A List<String> is a list of Strings, it has a method "void add(String)" and a method "String get(int)".
    A List<File> is a list of Files, it has a method "void add(File)" and a method "File get(int)".
    List is just one class (interface actually but don't worry about that), but we can specify different type arguments which means the methods use this abstract type rather than a fixed concrete type in their declarations.
    Why?
    You spend a little more effort describing your types (List<String> instead of just List), and as a benefit, you, and anyone else who reads your code, and the compiler (which also reads your code) know more accurately the types of things. Because more detail is known, the compiler is able to tell you when you screw up (as opposed to finding out at runtime). And people understand your code better.
    Once you get used to them, its a bit like the difference between black and white TV, and colour TV. When you see code that doesn't specify the type parameters, you just get the feeling that you are missing out on something. When I see an API with List as a return type or argument type, I think "List of what?". When I see List<String>, I know much more about that parameter or return type.
    Bruce

  • How to get the parameter name of a generic class?

    Is it possible to get the name of generic class parameter type without having an instance of that type?
    e.g.
    class MyClass<T> {
      public MyClass() {
      // get the name here
    }

    I need to get a string containing "Object" (or "java.lang.Object") if the class would be used like this:
    MyClass<Object> mc = new MyClass<Object>()

  • XI: How To Use JAVA generic Class to  perform SAP data Lookup........

    Hello All,
    I want to create a generic class which is used to perorm SAP data lookup.
    I don't want to use Jco or RFC channel..
    Is there any other way to do this?
    waiting for Reply 
    thank in advance.
    - AKSHAY.

    Hi,
    use RFC channel
    you can wrap it up like this:
    /people/morten.wittrock/blog/2006/03/30/wrapping-your-mapping-lookup-api-code-in-easy-to-use-java-classes
    why do you want to create something diffucult to maintin and non standard if
    you can use the RFC API ?
    Regards,
    michal
    <a href="/people/michal.krawczyk2/blog/2005/06/28/xipi-faq-frequently-asked-questions"><b>XI / PI FAQ - Frequently Asked Questions</b></a>

  • Question about typecast to more generic Class

    I have controls refnum and pages refnum and I want to know which way is better:  
    1) let LabVIEW doing the job
    or 2) use "typecast to more generic Class". 
    In both case the VI works properly. 
    Thanks
    Jean-Marc
    Jean-Marc
    LV2009 and LV2013
    Free PDF Report with iTextSharp
    Solved!
    Go to Solution.
    Attachments:
    Test LV86.zip ‏153 KB

    Thanks for your reply Ben.
    I need the controls refnum in the visible part of the pane (for the translation english to french and french to english ).
    Jean-Marc
    Jean-Marc
    LV2009 and LV2013
    Free PDF Report with iTextSharp
    Attachments:
    Translation LV86.zip ‏235 KB

  • Generic classes and the catch block

    Is there a reason why the catch block doesn't work with generic classes?

    JLS 8.1.2:
    <quote>
    It is a compile-time error if a generic class is a direct or indirect subclass of Throwable.
    </quote>
    <quote>
    This restriction is needed since the catch mechanism of the Java virtual machine works only with non-generic classes.
    </quote>
    I guess they didn't feel a need to tinker with the JVM definition to achieve that.
    Too slow!
    Message was edited by:
    DrLaszloJamf

Maybe you are looking for