Page cover image

Hierarchy

About

In Java, everything follows a hierarchy. At the very peak of this hierarchy sits the Object class. Every other class in Java is ultimately derived from this Object class. For example, every RuntimeException is also an Exception, which in turn is a Throwable, and ultimately, all of them are Objects.

Code organisation

Hierarchy helps us organize our code, showing us how different types of objects are connected to each other. By grouping related classes together, it allows us to define common behaviors and access shared features. As you explore further, you'll realize its significance in almost every aspect of the code you write. Whether you're creating simple programs or tackling complex projects, understanding and utilizing hierarchy is key to writing effective Java code.

Java's package system allows us to organize classes into logical groups. Packages can contain sub-packages, forming a hierarchical structure that mirrors the organization of our codebase. This helps in maintaining a clean and modular codebase by grouping related classes together and separating them from unrelated ones.

Inheritance

Java allows classes to inherit properties and behaviors from other classes through inheritance. This enables us to create a hierarchical relationship where subclasses inherit features from their superclasses. By structuring classes in this manner, we can establish a clear hierarchy that reflects the relationships between different types of objects in our code. Moving forward, let's explore a given example.

Here, we introduce the Cache class, designed to store a set of values efficiently. Beyond mere storage, it offers essential functionalities common to caches, including purging, fetching, updating, and checking for the existence of items. Furthermore, it extends the AbstractService class, which provides methods for starting and stopping the cache, enhancing its manageability and control over its lifecycle.

Within our hierarchical structure, the Cache class initially fulfilled the task of storing values efficiently. However, recognizing the need for periodic updates, its capabilities were extended by introducing a specialized class: RefreshableCache. This subclass inherits from Cache, inheriting its storage functionality, while augmenting it with the ability to schedule automatic refreshes. This hierarchical relationship enables a seamless integration of scheduling behavior into the existing cache infrastructure, promoting code reuse and modular design within our system.

Expanding on this concept, there might be various methods to refresh a cache, such as fetching data via REST API invocation or retrieving content from a file. This realization led to the creation of the RestRefreshableCache class, which specifically handles cache refreshing through REST API calls. In some instances, projects may need to fetch files via REST and transform their content into cached entities. To address this scenario, the FileRestRefreshableCache class was developed. Notably, you might observe the presence of an access token interface in the diagram. This interface facilitates the authorization of REST requests, ensuring secure communication when necessary.

In essence, the hierarchical structure of our system provides us with the flexibility to pragmatically develop components that can be easily extended to add additional functionality to existing objects. This approach fosters scalability and adaptability within our system architecture, allowing us to efficiently meet evolving requirements and integrate new features seamlessly

Encapsulation & Abstraction

Encapsulation shields the inner workings of classes, affording users interactions through clearly defined interfaces. Abstraction empowers developers to concentrate on core features while concealing extraneous intricacies. By structuring classes hierarchically, developers can bundle related functionalities, abstracting complexities for modular, maintainable code. For instance, in our cache example, the cache conceals its operational intricacies, possibly performing additional data analysis or transmitting data to a monitoring system, all transparent to users who are solely acquainted with the cache's public methods and functionalities.

Interfaces play a pivotal role in enhancing abstraction by defining a contract that implementing classes must adhere to, ensuring uniform treatment of disparate classes based on shared behavior.

In each application API module, the utilization of hierarchy will be extensive. Every API method will be associated with its unique pair of request-response messages, each ultimately extending from a base request/response structure. At the foundation of our request/responses, we can embed essential data that every request will carry, thereby centralizing common validation procedures for all requests and responses. This practice effectively minimizes code redundancy. Furthermore, by establishing a contract for all requests and responses, it guarantees consistency across the API, promoting robustness and reliability in our applications.

Summary

In Java development, the class hierarchy is heavily utilized, manifesting in various scenarios and contributing to the robustness and scalability of applications. Whether it's implementing inheritance, polymorphism, or abstraction, understanding and leveraging the class hierarchy is paramount to writing efficient and maintainable code.

In conclusion, the class hierarchy in Java is not just a concept—it's a powerful tool that underpins the foundation of software development, offering developers a framework to create elegant, extensible, and scalable solutions.

Last updated