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 trial

Java Java Objects Meet Objects Creating Classes

David Reese
David Reese
3,515 Points

Can someone please explain exactly what a class is- is it like a function, or a variable, or something??

I keep watching this over and over again and have no clue what a class is, and also what an object is. Whats the difference between this an simply making a function with some parameters?

4 Answers

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi David..

I admit it is hard for the first time learning Object Oriented programming concept. I also still struggling learning the whole concept of programming. Alright let's start with what a class is?

In the simplest term class can be seen as a template. This is the biggest difference between a functions (or in Java you will hear more it called by the name method) with parameters and classes. Functions job is limited to do computation of the parameters and depends on its specification give outputs from the computation. In the case of classes it serve more as a template. Inside a class there consist of attributes with state inside and functions defining how the class behave. Thus class can be used to define many objects through a process called instantiation.

Take an example of class called car. A car can have different color, different engine capacity, different brand, and so on. The car can be driven if you know the method to drive it. In this example car serve the purpose of defining what car is therefore it is a class. If you buy a car of your own you can choose the color, engine, brand etc. of it and I too can select my own color, engine, brand etc. So you have your own car and I have my own (different car) that means my car and your car are two different object but still defined as a car right? That is simple example of the relation between class and object.

The attribute in your car include color. This color is your own choice and you can modify it if you want and if you can. This is attribute which define the state of the object (your car) and it is part of class (car) definition. Then as for the drive aspect this can be seen as method that define how a car behave. The car can turn left or right depending how you turn the steer. This takes a function to define how the behavior of your car will be. It takes parameters from you like where you turn your steer and how deep you press the acceleration pedal to define the car behavior of direction and speed.

So inside of class (definition) of a car all of this like attribute such as color etc. and method (behavior) how it should be driven is defined. Therefore, every time the factory creates a car to be sold to customers it has to comply with the definition. I guess that is the best I can tell you about. I hope it still useful, and I am sorry if you get even more confused. Any other inputs are welcomed.

David Reese
David Reese
3,515 Points

First of all thanks SO much. But just to clarify, the class is the template, like a Pez dispenser without a type of head specified, or a car without an engine, color, company, or anything specified. The class is the definition, we both have cars and we bth use the same class. An object though is our specific cars, like my object might be a Honda and yours be a Toyota and they still be part of the same class. As far as the method, thats simply how the car is acting, and can be changed by the user.

Is that all correct? Thanks so much for your time.

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi David...

Yeah that's basically covers the simple definition of a class. However, for the method part it is simply how the car would behave or work but it is not exactly can be changed by the user. It is more appropriate to say the behavior of a method is depending on the input from user.

In our example of the car, the behavior of the car for instance: turn left or right are depending the input of the user by turning the steer left or right. The behavior of a car going faster is depending on the input from the user by pressing the acceleration pedal deeper.

David Reese
David Reese
3,515 Points

Thanks that's what I meant, I typed it awkwardly though. Thanks for your time.