Class Relationships in Java | Types of Relationships

Class relationships in Java define the special relationships among different kinds of classes. For example, there is a special relationship between a class named Vehicle and a class Car: A Car is a type of Vehicle.

Similarly, there are relationships among classes such as Shape, Circle, Rectangle, and Square. A Circle is a type of Shape, and a Rectangle is also a type of Shape.

When we design a large application or software system, it is important to identify and understand the relationships among classes. These relationships help you organize code efficiently and improve software design.

Example of Class Relationships in Java


For example, if several classes share common behaviors or methods in an application or program, we can save effort by placing the common behaviors (methods) within the superclass.

Similarly, if some classes are completely independent and unrelated to each other, we can assign different programmers to work on them separately without worrying that one of them will have to wait for the other.

For this purpose, we need to learn different types of relationships among classes in Java. Relationships among classes help to understand how objects in a program work together and communicate with each other.

Types of Relationship Among Classes in Java


There are three most common relationships among classes in Java:

  • Dependence (“Uses-A”) Relationship
  • Association (“Has-A”) Relationship
  • Inheritance (“Is-A”) Relationship

Class Relationships in Java

Association is further classified into aggregation and composition, which will be discussed in detail in later tutorials.

Let us understand these relationships one by one.

Dependence (Uses-A) Relationship in Java


When we create an object of a class inside a method of another class, this relationship is called dependence relationship in Java, or simply Uses-A relationship in Java.

In other words, if a method of one class creates or uses an object of another class to perform a task, the first class is said to depend on the second class. Dependency is one of the most common and general relationships in Java.

Look at the figure below where the display() method of class B creates or uses an object of class A. So, we can say that class B depends on class A if class B uses an object of class A.

Types of Class Relationships in Java

If several classes in an application depend heavily on each other, the coupling between classes is said to be high. Therefore, it is considered good programming practice to reduce dependency between classes (low coupling) because excessive dependencies make applications more difficult to maintain, test, and modify.

On the other hand, when classes have fewer dependencies, the coupling between classes is low, which improves flexibility, maintainability, and code reusability.

Why Does Coupling Matter?


If a class changes its behavior in the future release of an application, all the classes that depend on it may also be affected. In such cases, we may need to update the dependent classes as well.

Therefore, if the coupling between classes will be low, we can easily manage them. Hence, we should try to minimize unnecessary coupling between classes.

Association (Has-A) Relationship in Java


Association is another fundamental relationship between classes in Java, commonly known as the Has-A relationship.

When an object of one class is declared as a data member (instance variable) inside another class, the relationship is called an association relationship or simply Has-A relationship in Java.

Look at the figure above where class B contains an object of class A as its data member, then class B has a Has-A relationship with class A. This relationship is easy to understand and represents a stronger relationship than simple dependency because one object becomes part of another object.

Association in Java is further divided into:

  • Aggregation
  • Composition

We will learn the Has-A relationship in more detail in the next tutorial.

Inheritance (Is-A) Relationship in Java


Inheritance represents the Is-A relationship in Java. It establishes a relationship between a more general class (called the superclass or parent class) and a more specialized class (called the subclass or child class).

In other words, the Is-A relationship defines a relationship between two classes in which one class extends another class using the extends keyword.

Look at the figure above where a class B makes a relationship with class A by using the keyword “extends” and inherits data members and methods of class A.

Inheritance promotes:

  • Code reusability
  • Method overriding
  • Runtime polymorphism
  • Better code organization

For more details, refers to the tutorial: Inheritance in Java | Example, Use, Advantage

UML Notation in Java


UML stands for Unified Modeling Language. It is an internationally accepted standard notation used to visualize, design, and document software systems. Many programmers and software designers use UML diagrams to represent classes and the relationships among classes.

Java UML Notation

There are many tools available for drawing UML diagrams. The table shown in the figure below represents the UML notation used for class relationships in Java.


In this tutorial, we have discussed the important concepts related to relationships among classes in Java with the help of diagrams and explanations.

We hope this tutorial helped you understand these concepts clearly. Stay tuned for the next tutorial, where we will learn the Has-A relationship in Java with examples.

Thanks for reading!

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.