Spot the Differences: Java Chapter 06
This Java series covered a plethora of topics spanning from JDK to loops, and from methods to collections. These last few differences will mark the end of this series.
Method Overloading vs Method Overriding
Overloading occurs when an existing method is defined again but with different parameters, and the return type might not be the same. It may or may not involve inheritance. Overriding occurs when two methods are used with the same name and parameters, and always uses the concept of inheritance. In simple words, method overloading is a compile-time polymorphism that occurs within a class whereas method overriding is a run-time polymorphism that exists in two classes with an inheritance relationship. Private and final methods can be overloaded but not be overridden. The static method cannot be overridden but can be redeclared. Any method that cannot be inherited cannot be overridden.
Default Equals vs Overridden Equals
The default equals() method defined in the Object class in Java employs the == comparison operator by default. It checks for reference equality, i.e., it checks if the two object references point to the same memory location, indicating that they are the same object in memory. However, the equals method can be overridden to include customized logic in the comparison process. This can be done to provide a meaningful comparison based on the object’s content. The overridden equals method should abide by a set of rules — it must be reflexive, symmetric, transitive, consistent, and handle null values well.
Data Structures vs Collections
Both data structures and collections are used in Java to organize and store data, but they serve different purposes and have different characteristics. A data structure is a way of organizing and storing data in memory to facilitate efficient operations on that data. It describes how the data is systematized and accessed. Data structures can be primitive, like arrays and linked lists, or complex, like trees and graphs. Collections in Java refer to a framework of classes and interfaces provided by the Java Collections Framework (JCF) to manipulate and store groups of objects. Examples of Collection classes and interfaces include lists, sets, maps, queues, stacks, and dequeues.
Get Madhumitha S’s stories in your inbox
Join Medium for free to get updates from this writer.
Note:
A collection is an interface that groups objects into a single faction. Collections is a utility class that comprises a set of operations that can be effectuated on a Collection. Collections provide higher-level abstractions over data structures, making it easier to work with and manipulate data in various ways.
ArrayList vs LinkedList
Array lists and Linked lists are both implementations of the List interface in Java Collections Framework but their underlying data structures are different. An array list basically uses a dynamic array whereas a linked list uses a doubly-linked list where each node contains a value and references to the previous and next nodes. Insertion or deletion of elements is usually faster in a linked list except at the ends since arrays must shift neighbouring elements to accommodate a new element or remove an existing one. This will modify the entire layout of the array list. However, arrays perform traversal and random access faster since they can access an element using its index. Linked lists can access elements only after scrutinizing the references.
HashMap vs TreeMap
The two Map interface implementations in the Java Collections framework are used to store key-value pairs. Hash Map executes a hash table but TreeMap executes a red-black tree, a self-balancing binary search tree, because of which it results in ordered storage based on the natural storage of keys. A hash map does not store elements in any specific order and offers constant-time average performance for basic operations. However, this could be degraded by collisions or multiple keys having the same hash code. It permits one null key and multiple null values. On the other hand, a tree map provides consistent performance — O(log N) time complexity — regardless of the number of elements. It is the better method for sorted iteration over keys.
Note:
A tree map allows custom logic for the ordering of elements. Since a tree map relies on the natural order of keys or a custom comparator which would be undefined for null, it does not allow null values.
Congrats on making it to the end. Happy learning!








