Query regarding LIST interface and implementation.

interface List{

int size();

boolean isEmpty();

}

abstractclass AbstractListimplements List{

publicint size();

publicboolean isEmpty(){

return size() == 0;

}

}

publicclass ArrayListextends AbstractList(){

publicint size(){

return 0;// placeholder.

}

}

Query :

How can the abstract class AbstractList invoke size() == 0

in isEmpty(), when there is no size() implemented at all?

In ArrayList class, what is the meaning of the placeholder?

If I create an instance of ArrayList and invoke the size(),

wont I always be returned value of 0?

[1524 byte] By [bhuru_luthriaa] at [2007-11-27 8:36:47]
# 1

I don't quite get this. These are not snippets from the sources of java.util.List, java.util.AbstractList and java.util.ArrayList.

As to your question regarding how AbstractList's isEmpty method can invoke size(), AbstractList is abstract. As such it can not be instantiated, so when calling isEmpty, it will always invoke size() of some concrete extension of AbstractList. A concrete class can not have abstract methods and thus must provide an implementation of size().

dwga at 2007-7-12 20:33:50 > top of Java-index,Java Essentials,Training...