PDA

View Full Version : Classes & Objects....in Java......




fab5freddy
Feb 26, 2007, 02:19 PM
Hi There, I have just started learning Java. This is my first programming language. I am having a hard time grasping the concepts of classes & objects..... Is there anyone who can explain in a very simple way, the relationship between classes & objects...... THANKS !!



jeremy.king
Feb 26, 2007, 02:29 PM
A Class is a blueprint from which Objects are created. Objects are instances of a Class.

See http://java.sun.com/docs/books/tutorial/java/concepts/index.html

Heath
Feb 26, 2007, 06:31 PM
Consider the class as a definition for how something behaves and what type of information it can hold.

Now using that definition, you can create various things that will behave the same way and hold the same type of information. These would be objects.

There is only ever one class definition, but you can build many many objects using that one definition. Each object is unique and has its own information that is relative to it. However, each object will behave the same way since they were all created from the same definition.

Spritey
Mar 5, 2007, 12:14 AM
I remember I had some trouble grasping the concept too, but I think I got it after someone explained to me using a class called student

So you create a class called student which has variables such as

String sStudentName = "";
int iStudentNumber = 0;
double dGPA = 0;

The class called Student also has behaviors such as calculate GPA (which can be embedded as methods that can take parameters (i.e. grades being entered into a system, or another "Add Grades" class.

An object however, is calling the constructor (meaning you use the word 'new nameOfClassInThisCase:Student') and telling it that 'hey, we have a new Student at this school". Instead of having to create 10 000 classes (because there are 10000 students at this school) where you enter

public class Student1
{

String sStudentName = "Joe Jones";
int i StudentNumber = 1;
double dGPA = 3.0;
}
.
.
.

public class Student10000
{
String sStudentName = Samantha Simpson
int iStudentNumber = 10000;
}

As you can tell, they're an exact copy, except for that the variables contain different values. So what you do, is use a class called Student as a blueprint, and every time you have a new student you call on this blueprint, tell it that the student name is XX, the Student number is.. (or can be generated automatically). Thus, Samantha Simpson and Joe Jones are instances of the class Student (aka objects) and the variable values such as their names would in a case like this be stored in a database.

Hmmm... it's a lot simpler to explain this to someone in person, but I hope it made some kind of sense. (oh yeah, assume you know that my little example classes would under no circumstance run and are missing i.e. main method).

scan
Mar 7, 2007, 09:05 PM
I wouldn't be surprised if you still don't understand it from the replies. None of answers are simple, which is what you asked. I think I can help you understand.

Classes are like factories.

Objects are the things that come out of the factories.

Hi There, I have just started learning Java. This is my first programming language. I am having a hard time grasping the concepts of classes & objects..... Is there anyone who can explain in a very simple way, the relationship between classes & objects...... THANKS !!

Cue
Mar 8, 2007, 05:34 AM
Anything that has,


a name
properties (what makes up the object)
methods (what you can do with the object)
state

is basically an Object.

Think of a cookie (that would be the name) that has a size, shape (properties) and you can eat it (method).

At the beginning lets consider that we have a full sized rounded cookie (or that the values for its properties are: size = full, shape = round). That is the state that is currently in.

Now if we start eating it, we alter its size and shape. So now we have a half-sized half-rounded cookie! (a new state).
So by eating the cookie we alter its state.

*Theory: The methods of an Object alter the values of its properties thus creating a new state.

Now a class,


has a name
defines the object in terms of its properties and methods
In the cookie example, by defining we mean the fact that it has a size, a shape and that you can eat it.
what we use to create an object
Think that in order to make a rounded cookie we use a cookie form.

The fact that this cookie form makes cookies (obviously!) it means that we can make cookies that have size, shape and that we can eat them!

lazydog
Mar 8, 2007, 06:37 AM
Classes are like factories.

Objects are the things that come out of the factories.

I think that analogy is a little bit confusing. Real factories make other objects, they don't make factories, ie replicas of themeselves.

A class is a blueprint for an object. The 'new' operator creates an instance of the class through one of the class's constructors. If anything, the 'new' operator is the factory!

I know… I've probably only muddled things even more!

b e n

4np
Mar 8, 2007, 07:15 AM
Objects are instances of classes. Hence multiple objects can be instantiated using the same class.

oldpismo
Mar 8, 2007, 07:21 AM
Really simple example.

Person and Dog are both classes

John Smith and Dave Jones are both objects of class Person

Rex is an object of class Dog.

In the code you would have

public class Person
{
String personName;
int personAge;

public Person(String name, int age)
{
personName = name;
personAge = age;
}

public String getName()
{
return personName;
}

public int getAge()
{
return personAge;
}
}

Then elsewhere in the code you can create the objects

Person person1 = new Person("John Smith", 25);
Person person2 = new Person("Dave Jones", 37);

You have two objects of class person. you can get their names and ages by

String name = person1.getName();
int age = person1.getAge();
String name2 = person2.getName();
int age2 = person2.getAge();

I hope that helps, it turned out to be longer than I originally intended

jeremy.king
Mar 8, 2007, 09:53 AM
I wouldn't be surprised if you still don't understand it from the replies.

The link I provided is quite easy to understand and comes straight from Sun. See post #2.

scan
Mar 8, 2007, 01:11 PM
The link I provided is quite easy to understand and comes straight from Sun. See post #2.

I was more referring to the paragraphs and paragraphs of explanation from others, which is not what I think he is looking for.

scan
Mar 8, 2007, 01:17 PM
Well OBVIOUSLY a class is not a REAL factory. That's why I said "LIKE" a factory. Let's not get so technical with terms like 'new', etc.. He's obviously wanted a simple answer to understand it from a very abstract level, not a nitty, gritty, technical level.

Class is like a factory, which has guidlines/blueprints to make something (Objects).

I think that analogy is a little bit confusing. Real factories make other objects, they don't make factories, ie replicas of themeselves.

A class is a blueprint for an object. The 'new' operator creates an instance of the class through one of the class's constructors. If anything, the 'new' operator is the factory!

I know… I've probably only muddled things even more!

b e n

lazydog
Mar 8, 2007, 02:45 PM
Class is like a factory, which has guidlines/blueprints to make something (Objects).

No!!! A class does not make anything! If you want to use the analogy of blueprints and factories then 'new' is the factory that makes objects of a given class (the blueprint). What's so difficult about that??????????????

Anyway it's an analogy which wears thin once you get past the very very basics of OOP.

b e n

scan
Mar 8, 2007, 10:14 PM
No!!! A class does not make anything! If you want to use the analogy of blueprints and factories then 'new' is the factory that makes objects of a given class (the blueprint). What's so difficult about that??????????????

Anyway it's an analogy which wears thin once you get past the very very basics of OOP.

b e n

Please read what I say before you go misrepresenting my statement, again. I did not say Classes MAKE Objects. Also, like I said earlier, you're getting into too much detail and taking things way too literal. Obviously, classes are not factories. They don't have walls, employees, offices, or even a water cooler! But that's not the point!

The point is to help someone with an understanding of a concept. A class is like a factory; it contains the blueprints/specifications to make an object. Objects come out of the factory.

Using technical keywords like 'new' is not how to explain someone this concept. Imagine someone who has never heard of Java and know nothing about computers. That's how you should approach with the explanation.

lazydog
Mar 10, 2007, 05:21 AM
Classes are like factories.

Objects are the things that come out of the factories.

class is like a factory; it contains the blueprints/specifications to make an object. Objects come out of the factory.

From those two analogies I would imagine that most people would get the impression that classes make object.

The key point I was trying to make, which I though was important, is that in the relationship between classes and objects 'new' is the thing that transforms the first into the other.

b en