Fundamentals 6 min read

Understanding Composition and has-a Relationships in Java with Battery and Torch Example

This article explains Java class definition, interfaces, and composition, illustrating the has-a relationship through a Battery class used inside a Torch class, and discusses primitive types versus object types with complete code examples and execution results.

Java Captain
Java Captain
Java Captain
Understanding Composition and has-a Relationships in Java with Battery and Torch Example

We start by defining a class, which creates a new type, and an interface that describes how users can interact with objects of that type.

Composition allows a new class to contain objects of other classes, providing a basic mechanism for code reuse in Java. The "has-a" relationship is demonstrated by a Torch object that contains a Battery object as a data member.

Below is the implementation of the Battery class and the Torch class that composes a Battery instance.

class Battery { public void chargeBattery(double p) { if (this.power < 1.) { this.power = this.power + p; } } public boolean useBattery(double p) { if (this.power >= p) { this.power = this.power - p; return true; } else { this.power = 0.0; return false; } } private double power = 0.0; } class Torch { /** * 10% power per hour use * warning when out of power */ public void turnOn(int hours) { boolean usable; usable = this.theBattery.useBattery(hours * 0.1); if (usable != true) { System.out.println("No more usable, must charge!"); } } /** * 20% power per hour charge */ public void charge(int hours) { this.theBattery.chargeBattery(hours * 0.2); } /** composition */ private Battery theBattery = new Battery(); }

A Test class is provided to show how the composition works in practice.

public class Test { public static void main(String[] args) { Torch aTorch = new Torch(); System.out.println("Charge: 2 hours"); aTorch.charge(2); System.out.println("First Turn On: 3 hours"); aTorch.turnOn(3); System.out.println("Second Turn On: 3 hours"); aTorch.turnOn(3); } }

The program produces the following output:

Charge: 2 hours First Turn On: 3 hours Second Turn On: 3 hours No more usable, must charge!

The example demonstrates how composition enables the Torch to reuse Battery functionality, such as charging and checking remaining power, without duplicating code. It also touches on Java primitive types (int, float, double, boolean) versus object types, explaining that primitives are special classes with efficient memory allocation, while user‑defined classes require explicit instantiation with new .

In summary, the article covers composition (has‑a), primitive types, and the broader Java philosophy that "everything is an object," providing a concrete, runnable example to solidify these concepts.

JavaProgrammingobject-orientedCompositionhas-a
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.