1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Classes & Objects....in Java......

Discussion in 'Mac Programming' started by fab5freddy, Feb 26, 2007.

  1. macrumors 65816


    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 !!
  2. macrumors 603


  3. macrumors regular

    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.
  4. macrumors regular


    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).
  5. macrumors 6502

    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.

  6. Cue
    macrumors regular

    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!
  7. macrumors 6502a

    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
  8. 4np
    macrumors 6502a


    Objects are instances of classes. Hence multiple objects can be instantiated using the same class.
  9. macrumors member

    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
  10. macrumors 603


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

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

    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).

  13. macrumors 6502a

    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
  14. macrumors 6502

    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.
  15. macrumors 6502a

    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

Share This Page