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 trialMahvish Irfan
449 PointsWhat's the difference between method, class, argument and object?
Please explain in a very understandable way :) Also, where are they placed?
Thanks a lot!
3 Answers
Nathaniel McNeil
31 PointsMahvish,
A Class defines what an Object will look like; what attributes it will have. For example, you might define a "car" Class that has an attribute "doors" attribute, with the understanding that all cars have a number of doors.
class Car {
int doors = 0;
}
A Method can be thought of as a verb related to the class. For example, a Car deploys airbags in a collision. So we would define a Method that tells the Car how and when to deploy safety measures.
class Car {
int doors = 0;
void deployAirbags(speed, impactZone) {
if (speed > 20 and impactZone === 1) {
// deploy
}
}
}
An argument is data that gets passed to a function to make it work. In this example the arguments are 'speed' and 'impactZone'.
An Object is where we instantiate the Class as an actual car.
Car hondaAccord = new Car();
Then we can call the 'deployAirbags' on our 'hondaAccord' Object.
hondaAccord.deployAirbags(25, 1);
}
I hope this helps clear things up for you.
Nate
Mahvish Irfan
449 PointsNathaniel McNeil, thanks so much for your response. Greatly appreciated. I'm going to keep re-reading it until it makes more and more sense to me. Also, what do you mean by " instantiate the Class"?
Nathaniel McNeil
31 Points"Instantiate" means to implement. So, the Class defines generally what a Car is, and then when we instantiate/implement the Class, we are creating a specific instance of a Car. You might think of it like this: a Class is the design of a Car (drawings, models, specifications, etc..). Then, when a Car instantiated, it is rolling off of the manufacturing line as a Honda Accord (A specific and real implementation of the design; an Object). An Object, then, is an instance of a Class; an instantiated Class.
Clear as mud? :)
Mahvish Irfan
449 PointsNathaniel McNeil better :D this will take some time to absorb. I do have some additionalβ questions if you don't mind me asking
Nathaniel McNeil
31 PointsPlease do. I'd be happy to answer them:)