I am trying to convert my code into generic and I get an error at o1 + o2 stating that cannot find symbol for o1 and o2 then bad operand types for binary operator on the push(pop()+ pop()).

public abstract class MyStackGeneric <E> extends ArrayList <E>
{
    protected abstract E plus( E o1, E o2 );

    public void push (E i)
    {
        add (i);
    }

    public E pop () throws java.util.EmptyStackException
    {
        return remove (size()-1);
    }

    public void print(){
        ListIterator l = this.listIterator();

        while(l.hasNext())
        {
            System.out.print( l.next() + ", " );
        }

        System.out.println();
    }

    public E plus(E o)
    {
        return o1 + o2; //error here 
    }

    public void plus() throws ArithmeticException
    {
        if( size() < 2 ) throw new ArithmeticException("not enough element");
        push(pop()+ pop());//error here
    }

The type E could be anything, String, URL, JButton... so there's no version of the + operator that can be used with all of those. What did you want it to do?

o1 and o2 are defined for the abstract 2-arg plus method, but not for the 1-arg version where you get the undefined symbols.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.