Amidst defining the hierarchy, firstly, one can think to embed the abstract method(behavior) in abstract class
only because the derive concrete class possess that behavior as core behavior with it's specific implementation, secondly, one can think to embed the abstract method(behavior) in interface
only because derived concrete class possess that behavior as non-core behavior(peripheral) having it's specific implementation.
As I would not rely on this example which supports above point, below are the two references, I would rely on.
1) First reference that supports this point:
Interfaces are ideal for defining mixins. Loosely speaking, a mixin is a type that a class can implement in addition to its "primary type" to declare that it provides some optional behavior. For example,
Comparable
is a mixing interface that allows a class to declare that its instances are ordered with respect to other mutually comparable objects. Such an interface is called a mixin because it allows the optional functionality to be "mixed in" to the type's primary functionality. Abstract classes can't be sued to define mixins for the same reason that they can't be retrofitted onto existing classes: a class cannot have more than one parent, and there is no reasonable place in the class hierarchy to insert a mixin.
2) Second reference that support this point:
In UML sketch, the relation between interface
model and concrete class
model is named Realisation
, where as, the relation between abstract class
model and concrete class
model is named is-a
relation. Here is-a
can be told, when concrete class
has core behavior unlike realisation
relation.
So, with these two references, It looks list<E>
and Collection<E>
are abstract class
but not interface
.
With this above explanation, I would like to understand,
Why Collection<E>
and List<E>
is designed to be interface
in java.util
package?
Is my understanding correct?
Queue
as well? ALinkedList
is both, but it can't have double inheritance! – Ordous Nov 13 '14 at 18:40What specific behaviour would an array-backed list and a linked list have that is the same in implementation?
, am not sure, why the implementation of array-backed list and linked list will be same(for example, insertFront() say)?, they will be different but for same behaviour name(i.e., insertFront()). for your second question: Yes,Queue
should be an abstract classif it has its own core behaviour. if
LinkedListis trying to inherit core behaviours of both super types
Listand
Queue` then that is meaningless. – overexchange Nov 13 '14 at 18:58ArrayList
andLinkedList
is different. Am still not clear, How same/different implementation related to usage ofabstract class
? Because i would just declare aspublic abstract boolean insertFront();
in my abstract class and implement it differently in multiple subclasses? – overexchange Nov 13 '14 at 19:05abstract class
may not always have common implementations. semantically, it make sense to leave it to the derived subclass to implement it as shown in this example for update() method. – overexchange Nov 13 '14 at 19:30update
method in that class, the author tied its presence to those details - anyCritter
has anupdate
method, and thatupdate
method can rely on theCritter
having aLocation
, which is a concrete mutablePoint
. – Ordous Nov 13 '14 at 19:35defining common implementation detail
, then what would you termtoEncode()
in this same example that every Critter implements it for example -class Fish extends Critter implements IPeripheralBehaviour{}
. – overexchange Nov 13 '14 at 19:43abstract class
, a class cannot have more than one parent, and there is no reasonable place(other thaninterface
) in the class hierarchy to insert a mixin . This is the reason i removedtoEncode()
method fromCritter
class and placed inIPeripheralBehaviour
interface as per this example. Coming back to query, theList
is an interface because the abstract methods within are optional functionality atList
level. – overexchange Nov 14 '14 at 12:24