Logout

Home OOP HL Last Next

D.4.5

Define the term object reference.

 

Teaching Note:

As typified by simple classes that are self-referential.

 

Sample Question:

sdfsdfsf

JSR Notes:



There are two things in this assessment statement. "object reference" and "self-referential".

Object Reference

First there's the same old same old very important concept of an object actually being, at its core a memory address reference.

So:

Student s = new Student("Jane", 10);

makes a "reference" variable, which is actually/technically the address of where the Student object resides.

What is s technically/literally? s is a memory address, which points to the data associated with it. So yes, via dot notation (.getName( ) and .getGrade( )) we can access the data associated with s. And, yes, some place in memory, there is a String with the value of "Jane", and right beside it is an int, with the value of 10. But it's the address of where those two things reside which s actually is, so let's say FFFF1234.

or put another way:

So what is s, technically/literally? FFFF1234.

And what does s therefore represent; what useful information does it point to, that, using dot notation we can get at? The data residing at memory address FFFF1234, which is "Jane" and 10.

 

Self-referential Classes

This is the kind of class in which at least one of its attributes ia another instance of that class.

Here's a very simple example:

public class MyClass( ){
    int a = 0;
    String s = "not set yet";
    MyClass mc = null;

    public MyClass( ){
    }

    public MyClass(int a, String s, MyClass mc ){
        this.a = a;
        this.s = s;
        this.mc = mc;
    }
    //sets
    //gets
}

Above, we can classify the class as being a self-referential class, because one of its attributes is of the type that it is; specifically, the MyClass class has attribute of type MyClass.

Self-referential Class vs. Recursion

Don't confuse this with recursion (if you have looked at recursion to this point). It's similar in a way, but different, and each is used for a different purpose. With recursion, it is a method which calls another instance of itself, and this is done for the purposes of iteration. With self-refrerential classes, it is the class itself which has an instance that is of the same type of itself, and this is done for purposed of being able to locate another instance of the same class, usually as part of a list.

The Reason for Self-referential Classes

With the use of self-referential classes, these kind of objects can easily find and link to other objects of the same class, and thus form lists of those objects. This is what is done with the abstract data type (ADT) linked list, and others similar to it like binary search trees.

Lists

Lists are data structures in which instances of a certain type can link up to form an abstract data structure. This structure need not be made of instances which are physically side by side in memory, as is the case with arrays. Rather, through knowing where each next "link in the chain" is, in terms of memory, the full list can be navigated back and forth. The advantage of a list over an array is that the instances of the list can be scattered throughout memory; lists do not require one big contigous memory space, as arrays do.

Intro to How the Linking Works

To understand how this linking is possible, keep in mind that objects are technically/literally theselves a 32 or 64 bit address. (This is the same concept as is shown above with the instance of the Student class actually/literally being a reference. Yes, reference types point to the more generally useful information associated with them, like name and grade, but the object itself is an address reference.)

So in a linked list, a Node instance does not just reference useful information like student name and grade. Self-referential nodes also contain "references" to another Node instance. And it is with this reference to another instance of the same class type that it forms a link with it, and thus, step-by-step, link-by-link, forms a list.

See the next assessment statement for details of how this looks in practice.