The principle of Hierarchical Relationships in Object-Oriented Programming(OOP).
Fun fact: The concept of “inheritance” in OOP was inspired by the biological inheritance we observe in the animal kingdom. Just like how characteristics and traits are passed down from parents to their offspring in nature, OOP allows classes to inherit properties and behaviors from their parent classes.
With the trivia out of the way, let’s get right to the main point. The reason for this post (other than to flaunt my fun facts with you ) is to share a unique perspective of OOP that many developers fail to recognize, especially when trying to understand the structure and purpose of the OOP concepts for the first time. In my opinion Object Oriented Programming (OOP) is a means for building reliable systems via reusable, modular components that connect in a hierarchical structure.
That can be a mouthful, but if I should attempt to simplify it I would say; Object Oriented Programming allows us to create small parts or components of a system and fit them into one another to build a whole structure or a system.
Sorta like Lego.
My Lego analogy might have been off track, but if you already know the features of OOP then you know Object-oriented systems will be based on the principles of object-oriented programming (OOP), which is a programming paradigm that organizes software design around objects, objects which can contain data and behavior, objects created from a base blueprint, otherwise known as the Class. These objects are meant to store relevant data and behavior just like their real-world counterpart.
Now, this is the perfect segue to ask: Do you realize that Object-oriented programming (OOP) utilizes hierarchical relationships to organize and structure its classes and objects? It does my friend, it does. There is a clear hierarchical relationship established through inheritance, where classes are organized in a hierarchical tree-like structure. The classes higher in the hierarchy (superclasses or base classes) provide common properties and behaviors, which can be inherited by the classes lower in the hierarchy (subclasses or derived classes). This is a means to promote code reuse, modularity, and the ability to model real-world relationships.
Okay, check this out. Say, you want to model different types of vehicles. You can define a superclass called Vehicle
that provides common properties and behaviors for all types of vehicles. This way, you won't have to develop these behaviors or properties all over again in every subclass of Vehicle. Inheritance is true reusability if you ask me.
Check it, the Vehicle will become a means of inheritance:
public class Vehicle {
private String brand;
private String color;
private int year;
public Vehicle(String brand, String color, int year) {
this.brand = brand;
this.color = color;
this.year = year;
} public void accelerate() {
System.out.println("The vehicle is accelerating.");
} public void brake() {
System.out.println("The vehicle is braking.");
}
}
Now, let’s create two subclasses Car
and Motorcycle
that inherit from the Vehicle
class:
public class Car extends Vehicle {
private int numberOfDoors;
public Car(String brand, String color, int year, int numberOfDoors) {
super(brand, color, year);
this.numberOfDoors = numberOfDoors;
} public void openTrunk() {
System.out.println("Opening the car trunk.");
}
}public class Motorcycle extends Vehicle {
private boolean hasSidecar; public Motorcycle(String brand, String color, int year, boolean hasSidecar) {
super(brand, color, year);
this.hasSidecar = hasSidecar;
} public void doWheelie() {
System.out.println("Performing a wheelie with the motorcycle.");
}
}
Do you see? The Car
and Motorcycle
are subclasses of Vehicle
, inheriting the properties (brand
, color
, year
) and behaviors (accelerate()
, brake()
) from the superclass. That's not all, the properties and behavior inherited can be changed (Polymorphism) to match each object's uniqueness.
See how the constructors are used below to provide unique and specific data upon object creation. Now the objects; “car”, and “motorcycle” created below contain data and behavior that is used here to represent its real-world objects.
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", "Red", 2022, 4);
car.accelerate(); // Inherited from Vehicle class
car.brake(); // Inherited from Vehicle class
car.openTrunk(); // Unique to Car class
System.out.println(); Motorcycle motorcycle = new Motorcycle("Honda", "Black", 2021, false);
motorcycle.accelerate(); // Inherited from Vehicle class
motorcycle.brake(); // Inherited from Vehicle class
motorcycle.doWheelie(); // Unique to Motorcycle class
}
}
From the hierarchical relationships, you can see easily see how real-world objects can be modeled in Object Oriented Programming Systems by breaking down their relationships and replicating them in code. So whenever you find it difficult to decide how to model a Class or an entire system, just remember its real-world examples and their hierarchical relationships, because OOP’s got you covered.
NOTE: There are various means of demonstrating the hierarchical relationship of real-world objects with OOP. Some examples can be found below, these examples can act as a guide in seeing the link between Object-oriented systems and Real-world systems.
CLASS-TO-OBJECT HIERARCHICAL RELATIONSHIP
Animal Hierarchy:
- Class: Animal
- Objects: lion, tiger, leopard (objects of different types from the Animal class)
Vehicle Hierarchy:
- Class: Vehicle
- Objects: car, motorcycle, truck (objects of different types from the Vehicle class)
Shape Hierarchy:
- Class: Shape
- Objects: circle, rectangle, triangle (objects of different types from the Shape class)
Employee Hierarchy:
- Class: Employee
- Objects: manager, developer, designer (objects of different types from the Employee class)
“HAS-A” HIERARCHICAL RELATIONSHIP(Aggregation, Association, and Composition.)
Car Dealership Hierarchy:
- Class: CarDealership
- Association: CarDealership has Customers (Association)
- Aggregation: CarDealership has Salespeople (Aggregation)
- Composition: CarDealership has CarInventory (Composition)
Social Media Hierarchy:
- Class: SocialMediaPlatform
- Association: SocialMediaPlatform has Users (Association)
- Aggregation: SocialMediaPlatform has Posts (Aggregation)
- Composition: Post has Comments (Composition)
“IS-A” HIERARCHICAL RELATIONSHIPS(Inheritance)
Animal Hierarchy:
- Superclass: Animal
- Subclasses: Mammal, Reptile, Bird
- Further subclasses: Dog, Cat, Snake, Eagle
Vehicle Hierarchy:
- Superclass: Vehicle
- Subclasses: Car, Motorcycle, Truck
- Further subclasses: Sedan, SUV, SportsCar, Cruiser, PickupTruck