> For the complete documentation index, see [llms.txt](https://lukasz-nowakiewicz.gitbook.io/resources/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lukasz-nowakiewicz.gitbook.io/resources/python/oop.md).

# OOP

#### 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.

```
+----------------+       +----------------+
|     Car        |<>---->|    Engine      |
+----------------+       +----------------+
| - engine: Engine|      | - horsepower: int|
| + start()       |      +----------------+
+----------------+

class Engine:
    def __init__(self, horsepower):
        self.horsepower = horsepower

class Car:
    def __init__(self, horsepower):
        self.engine = Engine(horsepower)

    def start(self):
        print(f"Car with {self.engine.horsepower} HP started")

# Example usage
car = Car(150)
car.start()
```

#### Uses (Dependency)

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

```
+----------------+       +----------------+
|    Printer     |<-----|    Document     |
+----------------+       +----------------+
| + print(doc: Document)| | - content: str |
+----------------+       +----------------+

class Document:
    def __init__(self, content):
        self.content = content

class Printer:
    def print(self, document):
        print(f"Printing document: {document.content}")

# Example usage
doc = Document("Hello, World!")
printer = Printer()
printer.print(doc)

```

#### Inheritance

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

```
+----------------+       +----------------+
|    Animal      |<|-----|     Dog        |
+----------------+       +----------------+
| - name: str    |       | - breed: str   |
| + speak()      |       +----------------+
+----------------+

class Animal:
    def __init__(self, name):
        self.name = name

    def speak(self):
        pass

class Dog(Animal):
    def __init__(self, name, breed):
        super().__init__(name)
        self.breed = breed

    def speak(self):
        return "Woof!"

# Example usage
dog = Dog("Buddy", "Golden Retriever")
print(dog.speak())
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://lukasz-nowakiewicz.gitbook.io/resources/python/oop.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
