Welcome to the Treehouse Community
Looking to learn something new?
Suwijuk Nawaphanarat2,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!
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))
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
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 :)