Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 26, 2007, 03:19 PM   #1
fab5freddy
macrumors 65816
 
fab5freddy's Avatar
 
Join Date: Jan 2007
Location: Heaven or Hell
Classes & Objects....in Java......

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 !!
fab5freddy is offline   0 Reply With Quote
Old Feb 26, 2007, 03:29 PM   #2
jeremy.king
macrumors 603
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
A Class is a blueprint from which Objects are created. Objects are instances of a Class.

See http://java.sun.com/docs/books/tutor...pts/index.html
jeremy.king is offline   0 Reply With Quote
Old Feb 26, 2007, 07:31 PM   #3
Heath
macrumors regular
 
Join Date: Aug 2005
Location: Canada
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.
Heath is offline   0 Reply With Quote
Old Mar 5, 2007, 01:14 AM   #4
Spritey
macrumors regular
 
Join Date: Sep 2006
Location: Canada/Norway/USA
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).
__________________
I believe humans get a lot done, not because we're smart, but because we have thumbs so we can make coffee. ~Flash Rosenberg
Spritey is offline   0 Reply With Quote
Old Mar 7, 2007, 10:05 PM   #5
scan
macrumors 6502
 
Join Date: Oct 2005
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.

Quote:
Originally Posted by fab5freddy View Post
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 !!
scan is offline   0 Reply With Quote
Old Mar 8, 2007, 06:34 AM   #6
Cue
macrumors regular
 
Join Date: Mar 2005
Location: Edinburgh, UK
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!
__________________
Twitter
Cue is offline   0 Reply With Quote
Old Mar 8, 2007, 07:37 AM   #7
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Quote:
Originally Posted by scan View Post
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
lazydog is offline   0 Reply With Quote
Old Mar 8, 2007, 08:15 AM   #8
4np
macrumors 6502a
 
4np's Avatar
 
Join Date: Feb 2005
Location: The Netherlands
Objects are instances of classes. Hence multiple objects can be instantiated using the same class.
4np is offline   0 Reply With Quote
Old Mar 8, 2007, 08:21 AM   #9
oldpismo
macrumors member
 
Join Date: Aug 2003
Location: UK
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
__________________
20" G5 iMac, late 2008 2.4GHz MacBook
oldpismo is offline   0 Reply With Quote
Old Mar 8, 2007, 10:53 AM   #10
jeremy.king
macrumors 603
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
Quote:
Originally Posted by scan View Post
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.
jeremy.king is offline   0 Reply With Quote
Old Mar 8, 2007, 02:11 PM   #11
scan
macrumors 6502
 
Join Date: Oct 2005
Quote:
Originally Posted by kingjr3 View Post
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 is offline   0 Reply With Quote
Old Mar 8, 2007, 02:17 PM   #12
scan
macrumors 6502
 
Join Date: Oct 2005
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).

Quote:
Originally Posted by lazydog View Post
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
scan is offline   0 Reply With Quote
Old Mar 8, 2007, 03:45 PM   #13
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Quote:
Originally Posted by scan View Post
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
lazydog is offline   0 Reply With Quote
Old Mar 8, 2007, 11:14 PM   #14
scan
macrumors 6502
 
Join Date: Oct 2005
Quote:
Originally Posted by lazydog View Post
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.
scan is offline   0 Reply With Quote
Old Mar 10, 2007, 06:21 AM   #15
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Quote:
Classes are like factories.

Objects are the things that come out of the factories.
Quote:
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
lazydog is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Safari 7 & Java ngaham OS X Mavericks (10.9) 9 Nov 28, 2013 01:26 AM
OpenGL ES: Vertex Buffer Objects and Vertex Array Objects Blakeasd iPhone/iPad Programming 2 Jul 13, 2013 12:24 AM
Chrome on OS X -- Java plugin / Java 7 vs Java 6 Issue booyahbooyah Mac Applications and Mac App Store 4 May 31, 2013 10:13 AM
Will Fusion cache a mysql db installation & other Java tools ? esoda MacBook Pro 9 Nov 22, 2012 05:15 AM
Understanding Objects, Classes and the MVC Tander iPhone/iPad Programming 18 Jun 7, 2012 03:01 PM

Forum Jump

All times are GMT -5. The time now is 09:39 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC