Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python Dice Roller Boards

Suwijuk Nawaphanarat
Suwijuk Nawaphanarat
2,103 Points

I need help for someone to explain what is Subclass

I keep forgetting what is Subclass every single time when the question asked me to create a new Subclass. Where is this Subclass in a Class function? I appreciate your help. Thanks!

boards.py
class Board:
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.cells = []
        for y in range(self.height):
            for x in range(self.width):
                self.cells.append((x, y))

2 Answers

In a more theoretical explanation, a SUB-CLASS, aka CHILD-CLASS, aka DERIVED-CLASS is a class that is a copy of a previously defined class, but with the intention of customizing it for a more specific purpose. The changes could be subtle or substantial. For instance, you could be building a banking application. You would probably have at least two different types of banking accounts in this application: (1) Checking and (2) Savings. Both account types have similar qualities, but have some differences as well. You could create a SUPER-CLASS, aka PARENT-CLASS, that would ENCAPSULATE (or contain) the similarities and then create SUB-CLASSES to handle the peculiarities of each specific account type. It would look something like this:

  • Account: ABSTRACT PARENT CLASS - has account number, balance, methods for withdrawal & deposit, etc.
    • CHECKING ACCOUNT - (has all the above, plus...), does not earn interest
    • SAVINGS ACCOUNT - (has the same as Account Class, plus...) does earn interest

Hi there!

I believe we're talking about an inherited class, or child class. So a subclass wouldn't be inside the Board class you have there, but a class that inherits from it:

class Plank(Board):
    """
    A narrow type of board
    """

    def __init__(self):
        super().__init__(2, 4)

Hope it helps :)