OOP Associations
In object-oriented programming (OOP), associations represent relationships between classes. Associations can be categorized as one-to-one, one-to-many, or many-to-many.
One-to-many association - The association is established by adding books to the library and setting the library reference in the book.
Here's a simple Java example demonstrating a one-to-many association between two classes: `Library` and `Book`.
Library Class:
import java.util.ArrayList;
import java.util.List;
public class Library {
private String name;
private List<Book> books;
public Library(String name) {
this.name = name;
this.books = new ArrayList<>();
}
public void addBook(Book book) {
books.add(book);
book.setLibrary(this); // Establishing the association from Book to Library
}
public void displayBooks() {
System.out.println("Books in " + name + ":");
for (Book book : books) {
System.out.println("- " + book.getTitle());
}
}
}
Book Class:
public class Book {
private String title;
private Library library; // Reference to the Library class
public Book(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
public Library getLibrary() {
return library;
}
public void setLibrary(Library library) {
this.library = library;
}
}
Example Usage:
public class Main {
public static void main(String[] args) {
// Create a Library
Library library = new Library("City Library");
// Create Books
Book book1 = new Book("The Great Gatsby");
Book book2 = new Book("To Kill a Mockingbird");
Book book3 = new Book("1984");
// Add Books to the Library
library.addBook(book1);
library.addBook(book2);
library.addBook(book3);
// Display Books in the Library
library.displayBooks();
// Check which library a book belongs to
System.out.println(book1.getTitle() + " is in " + book1.getLibrary().getName());
}
}
In this example, the `Library` class has a one-to-many association with the `Book` class. Each `Library` can have multiple `Book` objects associated with it, and each `Book` object is associated with one `Library`. The association is established by adding books to the library and setting the library reference in the book.
This simple example illustrates the concept of associations in OOP, where objects from one class are related to objects from another class. Associations help model real-world relationships in a software system.
One-to-one association - The association is established by setting the address for a person.
Another example with a different scenario. This time, we'll model a one-to-one association between a `Person` and an `Address` in Java.
Address Class:
public class Address {
private String street;
private String city;
private String state;
private String zipCode;
public Address(String street, String city, String state, String zipCode) {
this.street = street;
this.city = city;
this.state = state;
this.zipCode = zipCode;
}
public String getFullAddress() {
return street + ", " + city + ", " + state + " " + zipCode;
}
}
Person Class:
public class Person {
private String name;
private int age;
private Address address; // Reference to the Address class
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
Example Usage:
public class Main {
public static void main(String[] args) {
// Create an Address
Address address = new Address("123 Main St", "Cityville", "Stateville", "12345");
// Create a Person
Person person = new Person("John Doe", 30);
// Set the Address for the Person
person.setAddress(address);
// Display Person's information including the Address
System.out.println("Person Information:");
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
System.out.println("Address: " + person.getAddress().getFullAddress());
}
}
In this example, a `Person` has a one-to-one association with an `Address`. Each `Person` object can have an associated `Address`, and each `Address` object is associated with one `Person`. The association is established by setting the address for a person.
This demonstrates how you can model real-world relationships using one-to-one associations in object-oriented programming. Associations allow you to represent connections between different classes, enhancing the ability to model complex systems.
No comments:
Post a Comment