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 trialthomascawthorn
22,986 PointsHas anyone got a really good analogy or simple explanation for what a 'class' is?
I'm referring to this PHP video, but I've got a feeling I came across it in Rails too..
Randy describes it in this video but I can't quite wrap my head around it. Can anyone help me out with an alternative description?
1 Answer
Andrew McCormick
17,730 PointsKind of the cliche example but suppose you are building houses, a class is like starting with a blueprint. If you decide to have both three- and four-bedroom houses, you can start with a blueprint (aka a class) called the house blueprint that has a ground floor and a second floor, but has no indoor walls drawn on the second floor. Then, you make two more secondfloor blueprints — one for the three-bedroom house and another for the fourbedroom house. (You name these new blueprints the three-bedroom house blueprint and the four-bedroom house blueprint.) So these other blueprints called 'three-bedroom' and 'four-bedroom' only describe the second floor, because they reference back to (or extend) the house blueprint which contains the info for the ground floor and the exterior of the second floor. So now rather than recreating all the directions for building the initial structure of a house, you can use your class (aka blueprint) to create many houses from one set of instructions.
*partially taken from Java For Dummies
thomascawthorn
22,986 Pointsthomascawthorn
22,986 PointsThis is fantastic! Thank you :)
So the class is a framework/blueprint to create objects - it tell objects what methods and properties it should include - but can you add properties and methods to an object that has been created from a class? i.e. customize it?
Andrew McCormick
17,730 PointsAndrew McCormick
17,730 Pointsshort answer: yes
long answer: Extending objects with new methods at runtime