Publishing System Settings Logout Login Register
What is a class and what is an object?
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on October 12th, 2009
352 views
Java Development

 

Object Oriented Programming (O.O.P.) is all around 2 basic definitions: Classes and Objects. Everything in O.O.P. is a class and every objects are made from classes. This paradigm tries to model real life making an abstraction of objects named a class. Ofcourse O.O.P. has many things around it, but they all go around this 2 main concepts. Later on I might explain more detailed caracteristics as polymorphism, inheritance and so on.

Classes

Putting this as simple as it can get, classes are nouns. Get any sentence and just identify the noun from that sentence, and you´ll get a class. (There might be exceptions, but 99% of the time classes are made out of nouns.)

Example: The cat is fat.

Here, the noun is "cat" so we can make a class out of it. Fat in this case will be a cat attribute. How do we model attributes. Well in this case we can use a boolean value like this:

boolean fat = true;

So we are telling the cat is fat, and fat is an attribute of cat.

So this is all about nouns. As easy as it sounds will make a class right now out of a sentence:

The blue car is running at 100mph.

Now we can make a class "CAR" (thats the noun of the sentence) and put 2 attributes and 1 method. The first attribute is "color". In this case color is blue, we can leave this as a String for instance. The second is speed, we can leave this as an integer or as a double, you decide. Last but not least is our method.  A methos is an action that a class has and when we instance a class as an object, the object can perform this action. The action here is "running". We are not going to get into actual code in here but you´ll see it later on in another tutorial, as for now you can imagine how running can be implemented.

CONGRATULATIONS!! You made up your first class. Although you haven't programmed it, you did it already in your mind. Now we will move up into objects.

Objects

Now we know classes are made from nouns. GREAT! But how about objects? Well to keep it simple just think about this: We are all "humans". Got it? "Human" is a class, and we are objects of that particular class. We all breath, talk, walk (well atleast thats the standard, I know some people can't, and thats sad). Those are methods from "Human" Class, and out hair style, tall height, weight, those are all atributes.

So... did you get it? An object is an instance of a class. So that makes us all different from each other, we are an instance with different attributes from each other. Thats as simple as it can be explain.

I hope you have understand this concepts and you like this tutorial. Please comment for feedback. Maybe my english isn't perfect, but my natural language is spanish, so please tell me my errors so I can improve. Thanks for your time reading. To finish this up I just want to give you an exercise. The only way of learning this paradigm is practicing, so here it goes.

Exercise:

Create a "HUMAN" class with 4 important attributes and 4 important methods, just to narrow things down, because Humans have alot of attributes and methods.

Hope you like this tutorial.

As I said before, pleas leave feedback.

Cheers.

unknown_gamer / LeX

Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
unknown_gamer
www.pixel-creations.org

I am a student of System Engineering. Programming is my pasion. JAVA is the language I've decided to be my main programming language although I know c#, VB.NET, php, GML, c++.
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Get Started Credit Corner Get Started Credit Corner