1

I have the following first generic class and its interface:

 public interface Generic1Interface<E extends Comparable> { .. } public class Generic1 <E extends Comparable> implements Generic1Interface<E> { .. //implementation of Doubly linked list using Node objects } 

And the second generic class and its interface:

 public interface Generic2Interface<E extends Comparable> { .. } public class Generic2 <E extends Comparable> implements Generic22Interface<E> { Generic1Interface<E> list; //so nothing here but a reference to an object of type Generic1Interface<E> Generic2() { list = new Generic1(); } 

Suppose we're working on a method inside the second class, and we try to instantiate a new instance of the second class, and name it "result", then try to access its Generic2 instance, it will give an error:

 public Generic2Interface<E> union (E a, E b) { Generic2Interface<E> result = new Generic2(); **result.list** = ....; 

result.list will give an error: "list cannot be resolved or is not a field". I think there must be a workaround to instantiate a generic class in a generic class. Thanks.

EDIT: I need to conform to the interfaces in each implemented class. So as you can see the method union has to return object of type Generic2Interface that's why I declare result like this.

3
  • 1
    Isn't a cast to Generic2 necessary before you try to access a field defined in that class? As of now, result is of type Generic2Interface.. Commented Oct 17, 2017 at 11:39
  • If I read your code correctly you try to access the list field on an object of type Generic2Interface. This field is not defined on the interface, but on the Generic2 class. Commented Oct 17, 2017 at 11:39
  • @RobObdeijn Correct. I forgot to mention the reason I'm doing this. I have to conform to the interface so I need to deal with instances of type Generic2 but at the end of each method in this class I have to return object type Generic2Interface<E> Commented Oct 17, 2017 at 13:06

1 Answer 1

1

You need a cast for result to Generic2 type. This works:

System.out.println(((Generic2)result).list);

Or this:

Generic2<E> result = new Generic2(); System.err.println(result.list); 
Sign up to request clarification or add additional context in comments.

6 Comments

I tried that but didn't work. What I actually tried was: Generic2Interface<E> result = new Generic2(); then result = (Generic2) result; but still I couldn't access result.list.
That's not what I've suggested. Set is from a different type hierarchy and isn't compatible with Generic2Interface. You need to use the type where you've defined the field list, which is only Generic2 and not any other type.
I need to conform to the interfaces, so I can't declare result like this: Generic2<E> result = ...
Not a problem, then you can use the first option i.e. wherever you need to access list, do a cast there to Generic2. The point still stands - you can only access list on a variable of type Generic2.
I understood your point. I tried casting, but it appears that a simple cast like this is not working: Generic2Interface<E> result = new Generic2(); then result = (Generic2) result; I still couldn't access list.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.