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 trialSergio Lopez
13,088 PointsWhat's the difference between subclasses and instances of a class?
They seem to be as similar as to be the same thing. Just curious.
2 Answers
Kenneth Love
Treehouse Guest Teacherclass Shape:
sides = 0
class Square(Shape):
sides = 4
diamond = Square()
diamond.rotate = 90
Square
is a subclass of Shape
. diamond
is an instance of Square
.
Zachary Smith
1,592 PointsSo in this case, Square would be given all the attributes a shape has with the exception that 'sides' is initialized to a difference number. If there was another attribute under the Square class, say 'length', then it would have all the attributes Shape has (that it inherited from) plus it's new attribute 'length'. Diamond on the other hand is an instance of Square. So it is given all the attributes of the Square class ('sides' and 'length').
Kenneth Love
Treehouse Guest TeacherCorrect.
Tristan Gaebler
6,204 PointsTristan Gaebler
6,204 PointsKenneth Love . How do they actually differ? Don't they have the same attributes?