Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Has 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?
Andrew McCormick17,729 Points
Kind 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