-1

I have three classes:

public abstract class fileHandler<Key extends Object, Value extends Object> {
}    

public A extends fileHandler<String, String[]> {
}

public B extends fileHandler<String, String> {
}

Now in my main function i do something like this:

fileHandler file= null;
If (<condition>) {
    fileHandler = new A();
} else
    fileHandler = new B():
}

But this gives 2 compile time errors:

  1. Cannot convert A to fileHandler
  2. Cannot convert B to fileHandler

How can I get rid of these errors as I don't get this error if base class is not generic.

Update:

My class hierarchy is:

  1. class fileHandler<Key, Value> { }
  2. class A extends fileHandler<String, String[]> { }
  3. class B extends fileHandler<String, String> { }
  4. class C that calls function gen(object of A) or
  5. class D that calls function gen(object of B).
  6. both C and D are derived from abstract class E.

Now how should I define these functions in C and D and E:

I gave the following:

E:

public abstract void gen (fileHandler A) throws exception;

C:

void gen (fileHandler A) throws exception;

D:

void gen (fileHandler A) throws exception;

C, D, and E give error fileHandler is raw type. Reference to generic type fileHandler(Key, Value) should be parameterized.

8
  • may I ask, what extends Object is useful for? Mar 25, 2011 at 22:36
  • If you don't capitalize your class name, it is not Java. Mar 25, 2011 at 22:41
  • yes but i got formatting all wrong in that question
    – user656189
    Mar 25, 2011 at 23:31
  • @user unknown - removed Object
    – user656189
    Mar 25, 2011 at 23:38
  • 2
    Edit the original question instead of cluttering the site with duplicates. Mar 26, 2011 at 0:37

4 Answers 4

3

When you instantiate a generic type with different type arguments, the two instantiations are not type compatible. Different instantiations of a generic type are not type-equivalent in the way that a class that inherits from another class is type-equivalent to that class, even when the type arguments themselves may be related by inheritance. Genericity and polymorphism are two separate language features.

Your code is equivalent to the following:

// Java defaults unspecified type arguments to Object.
fileHandler<Object, Object> file;

if (...) {
   // Error: fileHandler<String, String> is not equivalent to fileHandler<Object, Object>
   file = new fileHandler<String, String>();
} else {
   // Error: fileHandler<String, String[]> is not equivalent to fileHandler<Object, Object>
   file = new fileHandler<String, String[]>();
}

If you truly want to isolate a polymorphic interface that is independent of the types used to instantiate your fileHandler class, then I would suggest you use an interface:

interface IFileHandler
{
    // If this interface needs to talk about keys and values, it
    // does so using only java.lang.Object.
}

public class fileHandler<Key extends Object, Value extends Object>
    implements IFileHandler
{
    // ...
}

public class A extends fileHandler<String, String>
{
    // ...
}

public class B extends fileHandler<String, String[]>
{
    // ...
}

IFileHandler file;

if (...) {
    file = new A();
} else {
    file = new B();
}
3
  • what would interface contain in this case ?
    – user656189
    Mar 25, 2011 at 23:11
  • This was very helpful. I re-wrote using interface and everything worked perfect !!
    – user656189
    Mar 26, 2011 at 0:09
  • @user###: Glad to be of service. Apr 26, 2011 at 2:13
1

I get no compile error... if I fix all the error in your provided code

  class fileHandler<Key, Value> {
    }

  class A extends fileHandler<String, String[]> {
    }

  class B extends fileHandler<String, String> {
    }

then

fileHandler file= null;
if (/* condition */) 
{
    file = new A();
} 
else
{
    file = new B();
}
1
  • please cooment on my latest query on this bug at the end
    – user656189
    Mar 25, 2011 at 23:39
1

The problem is that you're trying to assign an instance to the class name. You should instead be saying

if (condition) {
   file = new A();    
} else {
   file = new B();
}

"file" is the reference here "fileHandler" is the class name.

Cheers.

1
  • The same still holds. Also as an aside Java classes are usually named using StudlyCaps (i.e. FileHandler) Mar 25, 2011 at 22:42
0

How you decide to fix this warning really depends on what you are trying to do and how strictly you want to restrict the allowed types for the parameter of gen().

Example 1

Very loose

abstract class E {
    public abstract void gen(fileHandler<?,?> A) throws Exception;
}

class C extends E {
    @Override
    public void gen(fileHandler<?,?> A) throws Exception {}
}

Example 2

Restrict Key generic to be a String

abstract class E {
    public abstract void gen(fileHandler<String,?> A) throws Exception;
}

class C extends E {
    @Override
    public void gen(fileHandler<String,?> A) throws Exception {}
}

Example 3

You probably want to name the unrestricted type so you can refer to it.

abstract class E {
    public abstract <Value> void gen(fileHandler<String,Value> A) throws Exception;
}

class C extends E {
    @Override
    public <Value> void gen(fileHandler<String,Value> A) throws Exception {}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.