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:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.