< Previous Next >
OOPS Tutorial
1. Overview
In this article, we will learn the important object-oriented concept Association.
It represents a relationship between two or more objects where all objects have their own life cycle and there is no owner. The name of an association specifies the nature of the relationship between objects.
2. Intent/Definition
It represents a relationship between two or more objects where all objects have their own life cycle and there is no owner. The name of an association specifies the nature of the relationship between objects.
Association is a relation between two separate classes which establishes through their Objects. Association can be one-to-one, one-to-many, many-to-one, many-to-many.
In Object-Oriented programming, an Object communicates to other Object to use functionality and services provided by that object.
There are two forms of association
Let’s take an example of the relationship between Teacher and Student. Multiple students can associate with a single teacher and a single student can associate with multiple teachers. But there is no ownership between the objects and both have their own lifecycle. Both can be created and deleted independently.
3. Implementation
public class Teacher {
private String name;
private List<Student> students;
// getter and setter methods
}
public class Student {
private String name;
private List<Teacher> teachers;
// getter and setter methods
}
Related Oops Posts
Top Core Java Tutorials
- Java Tutorial for Beginners
- 50 Java Keywords
- JDBC 4.2 Tutorial
- All Java/J2EE Tutorial
- Java 8 Tutorial
- Java Collections Tutorial
- Java Exceptions Tutorial
- Java Generics Tutorial
- Java 8 Stream API Tutorial
- Java Wrapper Classes
- Java Arrays Guide
- Java Multithreading Tutorial
- Java Concurrency Tutorial
- Oops Concepts Tutorial
- Java String API Guide
- Java Reflection API Tutorial
- Java I/O Tutorial
- Date and Time API Tutorial
- JUnit 5 Tutorial
- JUnit 4 Tutorial
- Java XML Tutorial
- Google GSON Tutorial
Top Java EE Tutorials
- Spring Security Tutorial
- RabbitMQ Tutorial
- Hibernate ORM 5
- Java Persistence API
- Spring Boot 2 Tutorial
- Spring Core 5 Tutorial
- Spring MVC 5 Tutorial
- Spring Data JPA Tutorial
- Apache HttpClient Tutorial
- Spring Framework 5
- Apache Maven Tutorial
- JAX-RS Tutorial
- Jersey Rest Tutorial
Java Library Tutorials
- Java API Guides
- Java SQL Package Tutorial
- All Java/J2EE Tutorial
- Java Lang Package Tutorial
- Java Util Package Tutorial
- Java Lang Reflect Package Tutorial
- Java Time Package Tutorial
- Java IO Package Tutorial
Top Java Best Practices
- Java Enums and Annotations Best Practices
- Java Generics Best Practices
- JUnit Framework Best Practices
- Java Naming Conventions
- Single Responsibility Principle
- Liskov's Substitution Principle
- Interface Segregation Principle
- Dependency Inversion Principle
- Open Closed Principle
- Oops principles in java
- Restful API Best Practices
- JSP Best Practices
- Guide to JDBC Best Practices
- Collection Best Practices
- String Best Practices in Java
- Exception Handling Best Practices
- Synchronization Best Practices
- Guide to JDBC Best Practices
- Serialization Best Practices
The source code of this post is available on GitHub: Object-Oriented Design Guide
Comments
Post a Comment