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.

Java

Mahvish Irfan
Mahvish Irfan
449 Points

What'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

Mahvish,

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
Mahvish Irfan
449 Points

Nathaniel 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"?

"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
Mahvish Irfan
449 Points

Nathaniel McNeil better :D this will take some time to absorb. I do have some additional​ questions if you don't mind me asking

Please do. I'd be happy to answer them:)