Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) Inheritance based stack implementation The Stack is implemented by extending

ID: 3758363 • Letter: A

Question

a) Inheritance based stack implementation

The Stack is implemented by extending Java Collection Framework (JCF) class ArrayList:

public class Stack<E> extends ArrayList<E> {

     // ... stack implementation goes here

}

b) Composition based stack implementation

The Stack is implemented by defining a JCF ArrayList object as instance variable of the class Stack:

public class Stack<E> {

private ArrayList<E> stk = new ArrayList<E>();
   // ... the rest of the stack implementation goes here

}

Discuss the advantages and disadvantages for each of the two Stack implementations.

Explanation / Answer

sol:

Composition vs. Inheritance

Inheritance Yields (Slightly) Better Performance:

Composition Yields Better Flexibility:

But: