Would you consider inheriting collection classes?

All concrete collection classes are inheritable because they are not final. But will you consider inheriting them to roll out your own custom collection class (say DualDeleteArrayList)? Under what circumstances is it recommended to perform such a customization to collection classes? In this article, we shall see some real time scenario where this should be done.

What is the benefit?

Before going into the examples where I have written/seen inheritance of collection classes in live applications, let us understand the benefits of the same. ArrayList, HashMap and HashSet offer a set of operations which are inline with common operations expected out of a collection of objects. For example, you may want to add, remove or iterate the objects of collection. The benefit of inheriting and overriding/adding your own methods in the sub class is that you have a new collection class which behaves the way you want it to.

If you have an application with say 5 million lines of code and you perform deletion of two consecutive objects from ArrayList or you perform deletion of two objects from end and then insertion of one object at the end very frequently for an ArrayList. In this case, it is better to inherit ArrayList class and use your own version of ArrayList because you will save a lot of repetitive code. You can name the custom class by adding a suffix to represent the new operation being added/overridden in the custom arraylist.

Sample Code

Now coming to code samples, following sample code inherits from ArrayList class and adds a new method named lastDoubleDeleteAdd. The purpose of this method is to delete two objects from end and then add the argument object at the end of List.

package com.example;

import java.util.ArrayList;

public class DeleteAddArrayList extends ArrayList{

    /** This method deletes two elements from the end of ArrayList and then adds a new element at the end.
     *
     * @param o
     * @return returns true if all the operations were successful.
     */
    public boolean lastDoubleDeleteAdd(Object o) {
        this.remove(this.size()-1);
        this.remove(this.size()-1);
        return this.add(o);
    }
}

The Downside

The disadvantage of having such custom classes in your application is the learning curve for new members in the team. Not everyone would have inherited collection classes to create their own custom collection. If you are providing code walk through to the new members in team then you should make it a point to inform them about such customizations. Also, it is essential that you add proper documentation to sub classes with details on the need of inheritance and any usage tips to watch out for.

One more disadvantage is that if you fail to override any super collection method then it could really bring your application in an unstable state. To avoid such scenario, first read the Javadoc of the method you want to override and understand the working of the method from super class.

Conclusion

If you look at the code of new method, you can see that we are using the API of ArrayList to perform the individual operations but we are combining three operations into one method invocations.
It is up-to your requirement and imagination, how you want to override and add new operations so that you avoid repetitive code.

Related Posts

Inheriting Collections classes to make custom classes admin Core Java
Would you consider inheriting collection classes? All concrete collection classes are inheritable because they are not final. But will you consider inheriting them to roll out your own custom collection class (say DualDeleteArrayList)? Under what circumstances is it recommended to perform such a customization to collection classes? In this article,...
<h2>Would you consider inheriting collection classes?</h2> <p>All concrete collection classes are inheritable because <a href="http://www.javaexperience.com/java-best-practice-avoid-final-for-tdd/">they are not final</a>. But will you consider inheriting them to roll out your own custom collection class (say DualDeleteArrayList)? Under what circumstances is it recommended to perform such a customization to collection classes? In this article, we shall see some real time scenario where this should be done.</p> <img src="http://www.javaexperience.com/wp-content/uploads/2012/09/custom-collections.jpg" alt="" title="custom collections" width="160" height="237" class="alignnone size-full wp-image-244" /> <h2>What is the benefit?</h2> <p>Before going into the examples where I have written/seen inheritance of collection classes in live applications, let us understand the benefits of the same. ArrayList, HashMap and HashSet offer a set of operations which are inline with common operations expected out of a collection of objects. For example, you may want to add, remove or iterate the objects of collection. The benefit of inheriting and overriding/adding your own methods in the sub class is that you have a new collection class which behaves the way you want it to.</p> <p>If you have an application with say 5 million lines of code and you perform deletion of two consecutive objects from ArrayList or you perform deletion of two objects from end and then insertion of one object at the end very frequently for an ArrayList. In this case, it is better to inherit ArrayList class and use your own version of ArrayList because you will save a lot of repetitive code. You can name the custom class by adding a suffix to represent the new operation being added/overridden in the custom arraylist.</p> <h2>Sample Code</h2> <p>Now coming to code samples, following sample code inherits from ArrayList class and adds a new method named lastDoubleDeleteAdd. The purpose of this method is to delete two objects from end and then add the argument object at the end of List.</p> 1 <h2>The Downside</h2> The disadvantage of having such custom classes in your application is the learning curve for new members in the team. Not everyone would have inherited collection classes to create their own custom collection. If you are providing code walk through to the new members in team then you should make it a point to inform them about such customizations. Also, it is essential that you add proper documentation to sub classes with details on the need of inheritance and any usage tips to watch out for. One more disadvantage is that if you fail to override any super collection method then it could really bring your application in an unstable state. To avoid such scenario, first read the Javadoc of the method you want to override and understand the working of the method from super class. <h2>Conclusion</h2> <p>If you look at the code of new method, you can see that we are using the API of ArrayList to perform the individual operations but we are combining three operations into one method invocations. It is up-to your requirement and imagination, how you want to override and add new operations so that you avoid repetitive code.</p>
Differences between Abstract class and interface
Java Database Connectivity
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments