Java Concept: Difference between objects and classes

Programming HOW TOs and in-depth guides for programmers of all levels. Programming is an essential skill for hackers, so start learning today!

Java Concept: Difference between objects and classes

Postby Aiden » Mon Sep 07, 2009 8:47 am

I'm going to go into an analogy now to help explain, so bear with me.

Think of a deck of cards. There's 54 cards in a deck. They all share similar properties. They all have a card width, a card height, a suit, and a number (or for face cards, letters.) Now, to represent any one Card, you would create one class that holds this data.
Code: Select all
public class Card { double cardWidth; double cardHeight; String suit; String numberOrFace; }
The above code can be thought of as a container of data. Instead of storing data in your program as a String or an Integer, you can now store data as a Card. However, with just the above code, we cannot yet create instances of Cards. We need something called a constructor.
Code: Select all
public class Card { double cardWidth; double cardHeight; String suit; String numberOrFace; public Card(String suitIn, String numberOrFaceIn) { cardWidth = 2.5; // measured in inches cardHeight = 3.5; // measured in inches suit = suitIn; numberOrFace = numberOrFaceIn; } }
The constructor for a class is declared by public <Class Name>() {. You can also have parameters passed to the constructor in the parentheses, just like we did above (suit and number/face value). In this case, we're working with playing cards, but the only pieces of data that change between cards is the suit and value of the card; the physical height and width of the card never change. In the above constructor, two parameters are expected to be passed: suitIn and numberOrFaceIn. Those values are assigned to suit and numberOrFace, respectively, and the cardWidth and cardHeight values are always the same for every card.

Classes are seperate from the main function of your program. At this point in time, my main program might look like this:
Code: Select all
public class Main { public static void main (String[] args) { System.out.println("Hello, world!"); // Create a Jack of Spades Card jack = new Card("Spades", "Jack"); // Create a ten of diamonds Card ten = new Card("Diamonds", "10"); } }
As you'll notice, you're using the same Card class to create multiple objects (also known as instances) of that class. A class is a data structure that stores data and methods in a specific way. An object is an instance of the class, the actual object. Another example of a Class/Object relationship would be a Person class, where two instances are created for Alice and Bob. This concept of reusing one class to create many objects (in this case, we would create 54 Cards) using one class is the difference between classes and objects, and is fundamental to programming in any object oriented language.
"When it takes forever to learn all the rules, no time is left for breaking them."
User avatar
Aiden
Administrator
 
Posts: 1080
Joined: Tue Oct 31, 2006 11:11 pm
Location: /usr/bin/perl

Return to ā€œ%sā€ Programming / Scripting Tutorials

Who is online

Users browsing this forum: No registered users and 0 guests

cron