
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
HAS-A Relationship in Java
Java is a programming language that follows the Object-Oriented Programming paradigm. One of the pillars of OOPs is Inheritance, where you can find a relationship between two classes as Parent and Child. This relationship allows these classes to reuse each other's attributes and methods.
Java classes can relate to each other in many ways such as, Is-A relationship and Has-A relationship. In this article, we are going to learn the concept of Has-A relationship in Java through examples.
HAS-A Relationship in Java
Has-A relationship is a type of association where two classes are linked to each other through objects. Here, one class contains an instance of another class. It is a unidirectional relationship.
These relationships are mainly based on the usage and they help to reduce duplication of code. It determines whether a certain class HAS-A a certain thing.
In OOP, the users do not need to bother about which object is doing the real work. One class can hide the implementation details from its users. Suppose the users ask the class to do a certain action. This class will either do the work by itself or ask another class to perform the action.
Types of Has-A-Relationship
There are two types of Has-A-relationship, which are-
Aggregation: It is a form of Has-A-relationship where one object contains another but, both should have independent lifecycles. The child can exist without a parent. Therefore, we can say aggregation is a weak type of association.
Composition: It is a form of Has-A-relationship where one object is a child of another, and cannot exist without the parent. The lifecycle of the parent is independent. Composition is a strong type of association.
Example of HAS-A Relationship
Let's look into a Java program that shows HAS-A relationship ?
import java.util.ArrayList; import java.util.List; // a file class to save file names class File { private String name; // constructor public File(String name) { this.name = name; } public String getName() { return name; } } // folder class to store files in it class Folder { private String name; private List<File> files; public Folder(String name) { this.name = name; this.files = new ArrayList<>(); } // method to add file names public void addFile(String fileName) { files.add(new File(fileName)); } // method to show file names public void showFiles() { System.out.println("Files in folder '" + name + "':"); for (File file : files) { System.out.println("- " + file.getName()); } } // to print message on folder deletion public void deleteFolder() { System.out.println("Folder '" + name + "' has been deleted, along with its files."); } } public class Example { public static void main(String[] args) { // creating object Folder myFolder = new Folder("Documents"); // adding files to the folder myFolder.addFile("Resume.pdf"); myFolder.addFile("Project.docx"); // showing files myFolder.showFiles(); // Deleting the folder myFolder.deleteFolder(); myFolder = null; // If you attempt to access the files now will cause an error // Uncomment the below line to try // myFolder.showFiles(); } }
Output
Files in folder 'Documents': - Resume.pdf - Project.docx Folder 'Documents' has been deleted, along with its files.
In the above code, the Folder class contains a list of File objects. When the folder is deleted, all files within it are removed. It shows Files cannot exist without a folder. It is an example of composition.