Question

First post so be gentle. :) I'm not sure what I am doing wrong here, hopefully someone can help me out.

I have an Class that implements the List interface. This class also has it's own method that will only add an item to the list if it is not already in the list. The trouble is that when I try to use my conditionalAdd method, I get a error stating that it can't find my method because it is looking for it in the WorkflowSubType class. Please see below:

When I instantiate the Class I am using:

List<WorkflowSubType> currentViolations = new Violations();

This is the definition of my class that implements the List interface:

import java.util.*;

public class Violations<E> implements List<E>{

public Violations() {}

public void conditionalAdd(E violation){
     if(violation != null)
     if(!this.contains(violation))
    add(violation);
}

@Override
public <T> T[] toArray(T[] a) {
return null;
}

@Override
public boolean add(E e) {
return false;
}

 @Override....

So how come I can't access the conditionalAdd method. The currentViolations object I created is a List, but it's also a Violations type. Am I correct in saying this?

Thanks in advance.
RW

Was it helpful?

Solution

You will need to cast your instance to Violations. If you don't do so, the compiler interprets it as a List, and a List does not have the conditionalAdd method. Do it like this: ((Violations<WorkflowSubType>)currentViolations).conditionalAdd(whatever);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top