Class & Object

edited April 2014 in Hello Processing

Dear all,

I'm a completely new beginner to Processing.

A class looks like this:

        Class Car{ //** CLASS NAME**

        color c;   // **CLASS DATA/VARIABLES**
        float xpos;
        float ypos;
        float xspeed;

        Car (color tempC, float tempXpos, float tempYpos, float tempXspeed){ // **CONSTRUCTOR**

        c = tempC;
        xpos = tempXpos;
        ypos = tempYpos;
        xpseed = tempXspeed;
        }

        void display(){ //**FUNCTIONS**
        stroke(0);
        fill(c);
        rectMode(CENTER);
        rect(xpos,ypos,20,10);
        }

        void move(){
        xpos = xpos + xspeed;
        if( xpos > width) {
        xpos = 0;
        }
        }
        }

Now my question is, does an object created from this class example, look like this:

Car myCar {

color c = color(255,0,0);
xpos = 0;
ypos = 100;
xspeed = 1;

}

void display(){ //**FUNCTIONS**
stroke(0);
fill(c);
rectMode(CENTER);
rect(xpos,ypos,20,10);
}

void move(){
xpos = xpos + xspeed;
if( xpos > width) {
xpos = 0;
}
}
}

I know what classes look like, but don't know what objects look like.

Do Objects look the same as the class, with data and functions, but minus the constructor?

I need help on this. Cheers!

Answers

  • you can auto-format your code in processing by pressing ctrl-t

    The tutorial is excellent

    The main idea is that a class is a cookie-maker and the object is the cookie.

    Therefore no need to repeat everything in the class for the object.

    Basically the object gets everything from the class. That's what it's all about (remember cookie and cookie-maker).

    So just say

    Car myCar ;

    and then

    myCar = new Car ( color(199,2,2), 123, 210, 4 );

    Greetings, Chrisir ;-)

  • edited April 2014

    Basically the only thing which actually ends up being instantiated are non-static fields.
    Every other class member stays bound to the original class. So it saves memory! O:-)
    Each instantiated object uses enough memory to have its own copy of each non-static field of its class! =:)

    // forum.processing.org/two/discussion/4631/class-object
    
    class Car {
      // Those are "static" fields. They're not instantiated.
      // Plus they're shared among all objects of this class.
      // And can even be accessed w/o creating objects 1st:
      static final color COLOUR = #0080F0;
      static final int MODE = CENTER;
    
      // Regular non-"static" fields.
      // Those are the only class members which are instantiated:
      int x, y, w, h, v;
    
      // Class constructor. Called when using "new".
      // Responsible to give initial values to fields.
      // It returns the newly instantiated reference "this":
      Car(int xx, int yy, int ww, int hh, int spd) {
        x = xx;
        y = yy;
        w = ww;
        h = hh;
        v = spd;
      }
    
      // Class methods. They aren't instantiated either:
      void display() {
        rectMode(MODE);
        fill(COLOUR);
        rect(x, y, w, h);
      }
    
      void move() {
        if ((x += v) > width - w/2)  x = -w/2;
      }
    }
    
  • did you get my post?

    it builds on the existing class Car and instantiates one car ( a cookie )

    so in order to make a car those lines are enough - the class is used for the car

    ;-)

  • edited April 2014

    I have read the tutorial already, as I have the Learning Processing Book in front of me (Chapter 8 - Objects).

    But still struggle to understand what an object actually looks like.

    From what I understood, the global variables, in non-OOP, it's written simply as:

        c color;
        float xpos;
        float ypos;
        float speed;
    

    With OOP, those variables are all put inside the Object. And instead of declaring each of those variables separately and initializing each one of them, we just declare the Car Object, and initialize the Car Object.

    So inside the Car Object, is the Global Variables.

    And instead of having functions defined in draw(), we have them all inside of the Car Class, and we call them from draw(), using the dot.syntax.

    Thus, Car myCar:

    Is a object declaration. A variable, of type 'Car'. It contains Global Variables, i.e;

    color c;
    float xpos;
    float ypos;
    float xspeed;
    

    myCar = new Car():

    This results in the global variables inside of the Object, to be initialised, thus creating the object.

    But the book mentions, that all the variables AND FUNCTIONS are taken out of the main program and stored in the Object. And that Objects are not primitives, but complex data types, as they store data AND functionality. So the Object also contains the methods?

    I am not sure if I am making any sense, I'm completely new to Processing and Programming.

  • all you say is ok

    the object gets also the methods from the class

    myCar is a global var

    c, xpos etc. are not global vars - they might have been but now they are within the class inherited by the object

    they can only be accessed via the object

    don't theorize so much, practice, try it...

    ;-)

  • edited April 2014

    Yes, myCar is a global variable, that contains variables that it gets from class Car (color c/float xpos etc), that would of otherwise been global variables (in non-OOP). I understand this part.

    The part I'm still unsure of, is whether myCar contains within it, the methods, because GoToLoop's post suggests it does not, and only the variables are inside the Object variable. But the book seems to indicate the Object variable contains both data and functions, unlike variables that hold primitive data types, which only has data inside the variable.

    So regarding the Object Variable, I only understand the data part. I'm stuck at the function part. Does it inherit functions from the class?

  • edited April 2014

    ... is whether myCar contains within it, the methods,
    because GoToLoop's post suggests it does not, and only the variables are inside the Object.

    Let's go part by part:

    1st, variables can only hold 1 value at a single time. Thus myCar can't contain a whole object, but merely its reference!
    An object's reference is created when it's instantiated by calling its class constructor using keyword new.
    That reference is internalized by keyword this inside that created object!

    As I've mentioned at my previous post, only non-static fields are actually instantiated.
    That is, individual copies of those fields are created for exclusive use for an object's instance!

    The other class members are still bound to the original class.
    However, they're accessed as if they belonged to the instantiated object itself!
    In short, instantiated objects inherit "everything" from its class in practice!

  • Objects are actually instances of classes, they are not separate code parts.

    If Car is a class with fields and methods, then when you do:

    Car car = new Car();
    

    the car variable will have a reference to a Car object, to an instance of the Car class.

    The class can be see as a kind of blueprint, while the object is the real thing, the physical instance with data in fields.

  • edited April 2014

    Yes, myCar is a global variable, that contains variables that it gets from class Car (color c/float xpos etc),
    that would of otherwise been global variables (in non-OOP). I understand this part.

    Java doesn't have "global" variables, but either field or local variables!
    That myCar variable is actually a non-static field of the sketch's top-class itself! /:)
    The sketch's class is instantiated, along w/ its non-static fields, at the very beginning.
    Much earlier before setup() is finally invoked by the "Animation" Thread! :-B

  • no need to look behind the scenes, gotoloop, or look at Java

    processing has global vars (in the sketch).

    If you like the object car can be understood as a global var.

    the properties of the car are not global vars but need to be accessed via car.

    It really doesn't make sense to explain a beginner the concept of processing and the super secret meta-class ;-)

  • Thus myCar can't contain a whole object, but merely its reference!

    Can you give me an example of a reference. I want to understand what will be contained within the myCar variable. From what I understood from everyone's replies here, it contains data/values that is the result of calling the constructor.

  • edited April 2014 Answer ✓

    In other even older languages, the term reference is called pointer instead.
    Actually it's a value that represents the 1st physical memory address of a contiguous block of memory!
    Thus, variables store just a tiny information (either 4 bytes in 32-bit or 8 bytes in 64-bit mode), enough to reach the actual object!
    It's through that stored memory address value "we" can call methods & access fields from a specific object!

  • edited April 2014 Answer ✓

    you wrote

    From what I understood from everyone's replies here, it contains data/values that is the result of calling the constructor.

    that's correct!

    it contains data and also the function (like drive or be displayed)

    that's very helpful for writing clear programs, that the data and functions come in one package

    this is the program from the tutorial (where all is said really)

    // Example: Two Car objects
    Car myCar1;
    Car myCar2; // Two objects!
    
    void setup() {
      size(200, 200);
      // Parameters go inside the parentheses when the object is constructed.
      myCar1 = new Car(color(255, 0, 0), 0, 100, 2); 
      myCar2 = new Car(color(0, 0, 255), 0, 10, 1);
    
      println ("myCar1 -------------------");
      println (myCar1.xpos);
      println (myCar1.ypos);
      println (myCar1.xspeed);
      println (" -------------------");
    }
    
    void draw() {
      background(255);
      myCar1.drive();
      myCar1.display();
      myCar2.drive();
      myCar2.display();
    }
    
    // =====================================
    
    // Even though there are multiple objects, we still only need one class. 
    // No matter how many cookies we make, only one cookie cutter is needed.
    class Car { 
      color c;
      float xpos;
      float ypos;
      float xspeed;
    
      // The Constructor is defined with arguments.
      Car(color tempC, float tempXpos, float tempYpos, float tempXspeed) { 
        c = tempC;
        xpos = tempXpos;
        ypos = tempYpos;
        xspeed = tempXspeed;
      }
    
      void display() {
        stroke(0);
        fill(c);
        rectMode(CENTER);
        rect(xpos, ypos, 20, 10);
      }
    
      void drive() {
        xpos = xpos + xspeed;
        if (xpos > width) {
          xpos = 0;
        }
      }
    }
    
    //
    

    I made some changes and it prints (at the end of setup()):

    myCar1 -------------------

    0.0

    100.0

    2.0

    --

    Listen, that's all simple when you've worked with it a few times. So just practice, draw some cars or balls or houses.

    Greetings, Chrisir ;-)

  • edited April 2014

    Okay, let me see if I understood this right.

    The myCar variable is a reference. A special variable, that stores the memory address of the object. At that memory address which it points to, it contains the data (as a result of calling the constructor) & functions, basically the details of the object?

    If that is correct, the memory address it points to, what is there that holds the data & functions? A variable?

    I hope this makes sense.

  • edited April 2014

    My previous explanation was focused on non-primitive variables.
    Which in turn store object references (memory addresses).
    While @Chrisir was focusing on the object itself I believe!

    The class is the matrix, the cookie-maker!
    We can instantiate as many cookies as we wish (or memory allows) outta it.

    However Java is smart and savvy! It'll only create copies outta non-static fields.
    Objects don't need their own copy of methods & static fields.

    They can still access them transparently from their original class after all!
    Re-read my 1st post! :P

  • I just edited my most recent comment above. Can you re-read that, and see if it's correct?

  • edited April 2014 Answer ✓

    The myCar variable is a reference.

    More precisely, it holds/stores 1 reference value of the declared type at a given moment.

    A special variable, that stores the memory address of the object.

    That's correct! But since Java is an OOP language, there's nothing special about it!
    Java provides both primitive & reference variables.
    There are exactly 8 primitive data-types. Anything else is an object data-type.

    While primitive variables store a concrete literal value, like -10L, 3.1415926535f, 1e3, 0300, 0xFF, #0080F0.
    Or some special abstract values like true, false, NaN, infinite.
    Non-primitive variables store the 1st physical memory address of objects.

    If that is correct, the memory address it points to, what is there that holds the data & functions? A variable?

    Objects themselves are a contiguous block of memory, dynamically allocated w/ sufficient space for its non-static fields,
    when a class constructor is invoked w/ reserved keyword new.
    The rest of the originator class members, i.e. methods & static fields, are accessed by instantiated objects transparently!

    In other words, classes themselves, like their objects, occupies a contiguous block of memory too!
    And they're complete w/ all kinds of members: static fields, non-static fields, constructors & methods!

  • edited April 2014

    Thanks. Is there somewhere that I can view the memory layout of objects & classes?

  • edited April 2014 Answer ✓

    There are 2 IDEs I know about which help OOP learning:

    They allow us to instantiate an object by clicking on them. And even see their current field values and invoke their methods!!!

    Plus a video class about programming in Java using GreenFoot:

  • Cheers, I've already got BlueJ, will look at the other now.

Sign In or Register to comment.