OOP

Object-oriented programming (OOP) concepts using Python pseudocode and UML diagrams.

Aggregation

Aggregation represents a "has-a" relationship where the child can exist independently of the parent.

+----------------+       +----------------+
|    Library     |<>---->|     Book       |
+----------------+       +----------------+
| - books: List  |       | - title: str   |
| + add_book()   |       | - author: str  |
+----------------+       +----------------+

class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

class Library:
    def __init__(self):
        self.books = []

    def add_book(self, book):
        self.books.append(book)

# Example usage
library = Library()
book1 = Book("1984", "George Orwell")
library.add_book(book1)

Composition

Composition represents a "contains-a" relationship where the child cannot exist independently of the parent.

Uses (Dependency)

A "uses" relationship indicates that one class uses another class as a parameter or local variable.

Inheritance

Inheritance represents an "is-a" relationship where one class inherits the attributes and methods of another class.

Last updated

Was this helpful?