9.4 Implementation Inheritance, default
Previously, we had an interface List61B that only had method headers identifying what List61B's should do. But, now we will see that we can write methods in List61B that already have their implementation filled out. These methods identify how hypernyms of List61B should behave.
In order to do this, you must include the default
keyword in the method signature.
If we define this method in List61B
Then everything that implements the List61B class can use the method!
However, there is one small inefficiency in this method. Can you catch it?
For an SLList, the get
method needs to jump through the entirety of the list. during each call. It's much better to just print while jumping through!
We want SLList to print a different way than the way specified in its interface. To do this, we need to override it. In SLList, we implement this method;
Now, whenever we call print() on an SLList, it will call this method instead of the one in List61B.
Last updated