🏠 Python OOP β€” Inner Class (Nested Class)

Introduction 🌟

An **Inner Class** (also called a **Nested Class**) is a class defined **inside another class**. It helps logically group classes that are only relevant within the outer class, improving structure and readability.

Note

πŸ’‘ Inner classes are often used when one class is strongly related to another
πŸ’‘ They help create modular, organized code
πŸ’‘ Syntax: class Outer: class Inner: ...

1. Basic Example of an Inner Class 🧱

basic_inner_class.py

class Outer:
    class Inner:
        def show(self):
            print("This is the Inner class")

o = Outer.Inner()
o.show()

βœ” Inner class is accessed using Outer.Inner()
βœ” Useful for grouping closely related logic

2. Outer Class Accessing Inner Class πŸ”—

outer_access_inner.py

class Outer:
    def __init__(self):
        self.inner = self.Inner()

    class Inner:
        def display(self):
            print("Inner class method")

o = Outer()
o.inner.display()

βœ” Outer class can create and use objects of the inner class

3. Inner Class Accessing Outer Class Attributes 🧠

inner_access_outer.py

class Outer:
    def __init__(self, value):
        self.value = value

    class Inner:
        def show(self, outer_obj):
            print("Outer value is:", outer_obj.value)

o = Outer(50)
inner = Outer.Inner()
inner.show(o)

βœ” Inner class receives the outer class instance as a parameter

4. Practical Example β€” Computer and CPU πŸ’»

computer_cpu.py

class Computer:
    def __init__(self):
        self.cpu = self.CPU()
        self.ram = self.RAM()

    def show_specs(self):
        print("CPU:", self.cpu.cores, "cores")
        print("RAM:", self.ram.size, "GB")

    class CPU:
        def __init__(self):
            self.cores = 8

    class RAM:
        def __init__(self):
            self.size = 16

comp = Computer()
comp.show_specs()

βœ” Inner classes help structure related components
βœ” CPU & RAM belong inside Computer β€” good encapsulation

5. Real-World Example β€” University System πŸŽ“

university_system.py

class University:
    def __init__(self, name):
        self.name = name
        self.department = self.Department()

    def show(self):
        print("University:", self.name)
        self.department.show()

    class Department:
        def show(self):
            print("Department: Computer Science")

u = University("Harvard")
u.show()

6. When Should You Use Inner Classes? 🎯

  • βœ” When the inner class is tightly coupled with the outer class
  • βœ” When an inner class should not be used independently
  • βœ” To group related logic in a clean, readable way
  • βœ” Useful for components (e.g., Engine inside Car)

7. Nested Classes and OOP Structure 🧩

Outer ClassInner Class
Represents main entityRepresents a part of that entity
Can create objects of inner classRarely used outside the outer class
Holds core logicHolds supporting logic

8. Inner Class Inside Method (Local Inner Class) πŸ§ͺ

local_inner_class.py

class Outer:
    def process(self):
        class LocalInner:
            def message(self):
                print("Local Inner Class")

        obj = LocalInner()
        obj.message()

o = Outer()
o.process()

βœ” Inner class exists only inside a method
βœ” Useful for encapsulating temporary or helper logic

9. Private Inner Classes πŸ”’

private_inner_class.py

class Outer:
    class __Inner:
        def show(self):
            print("Private Inner Class")

# Accessing private inner class (name-mangling)
inner = Outer._Outer__Inner()
inner.show()

Note

βœ” Makes inner class inaccessible from outside
βœ” Rarely used but important for security-sensitive code

10. Best Practices πŸ’‘

  • βœ” Use inner classes only when they are tightly bound to outer class
  • βœ” Avoid overusing them β€” can make code hard to read
  • βœ” Good for modeling components (Engine inside Car, CPU inside Computer)
  • βœ” Name inner classes clearly for readability

Conclusion πŸŽ‰

>>β€œInner Classes help create clean, structured code by grouping related objects inside a main class β€” making your OOP design more powerful and organized.” ✨

You now fully understand Inner Classes in Python! Want the next topic? Try Magic Methods, Composition, Interfaces, or Class Relationships. Just tell me! 😊