I have a question on how to have one method call work on two different classes of objects in Java? For example, if both classes SListNode and DListNode want to use the same method size() to measure the length of a list. I would like to define the size method only once, instead of writing out explicitly twice in both SListNode and DListNode classes.
I guess inheritance is necessary. For instance, define a superclass Size, which contains the size() method. Both SListNode and DListNode are subclasses of Size. However, a specific type of list is required in the definition of size() method in this way. Hence, the method cannot be applied to both SListNode and DListNode.
I was wondering what should the inheritance structure look like or if there is any standard way to solve this kind of question? I guess there is and this is why java has polymorphism. But it is hard for me to find out...