Examining the best practices in Java, we find avoiding inheritance is a good one. One of the reasons maybe illustrated in the following problem:
Here we have a sub-class "Stack" extends "ArrayList"
class Stack extends ArrayList
{ private int stack_pointer = 0;
public void push( Object article )
{ add( stack_pointer++, article );
}
public Object pop()
{ return remove( --stack_pointer );
}
public void push_many( Object[] articles )
{ for( int i = 0; i < articles.length; ++i )
push( articles[i] );
}
}
Let us say we are into adding to a stack using push()
defined in the previous code, and then we want to clear the stack using clear()
of the base-class -i.e ArrayList-
Stack a_stack = new Stack();
a_stack.push("1");
a_stack.push("2");
a_stack.clear();
- Here the problem is
The code successfully compiles, but since the base class doesn't know anything about the stack pointer, the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the stack_pointer's current value), so the stack effectively has three elements on it—the bottom two are garbage.
So my questions would be, why
base class doesn't know anything about the stack pointer
in other words, where is the state of the stack pointer being reserved?
source: Why extends is evil