Comp 110 Properties

Properties

Classes have properties. Each object made from a class will have all the properties established in that class. Additionally, each property in a class is a specific type of information about the class. So, all objects we create from a certain class will contain the properties defined in the class, and we can customize the values within these properties for each individual object. We define properties the same way we define variables, just without the let keyword. We can still explicitly give them a type, or rely on type inference when we declare and initialize them with a default value. Here's what this would look like inside a class:

class <ClassName> {
    <propertyName>: <type>;
    <otherProperty> = <default value>;
    // ...
}

//note: the first property in this template does not have a default value, and will, therefore, need to be assigned one!...the other property uses type inference to infer the variable type considering it is given a default value.

Here are some examples:

class ordinaryClass {
    stringProperty: string = "comp";
    numberProperty: number = 110;
    booleanProperty = false; // using type inference!
}
 
class TarHeel {
    name = "";
    studentPID = 0;
    hatesDook = true;
    // all the property declarations here used type inference!
}

The properties declared inside the body of the TarHeel class are namestudentPID, and hatesDook. Every TarHeel object we make will have these properties.

Within this class, we've set default values for each property. Each new TarHeel object we make will have properties that start with these default values ("" for name, 0 for studentPID, and true for hatesDook), but we can change the values for each object's properties later if we want.

Let's make a TarHeel object:

let ramster: TarHeel = new TarHeel();

Great! Since ramster is now a TarHeel object, it automatically has the properties set up in the class declaration with the default values we provided there. So, the name property of our ramster object is currently "", the studentPID property has the default value of 0 still, and hatesDook is true. We know our ramster object should have an actual name instead of the empty string "", so let's change it to make it what we want.

Assigning and Accessing Properties

In general, when we want to change properties of our objects so they don't always have the default values, we follow this format:

<object>.<property> = <value>;

We'll use the assignment operator (=) to change the properties in our ramster object so they can have their own unique values. 

To access an object's property, just use the name of the object followed by the dot operator (.) followed by the property name.

ramster.name = "Rameses";
ramster.studentPID = 123456789;

When we want to change the values of the properties that our ramster object has, we've got to be sure we tell the computer we're only talking about our specific ramster object. That's why we say ramster.name when we change the value of the name property. 

name = "Dookie";
//THIS NOT VALID! -- name is a property, not a normal variable name!
//even if we wanted to reassign all of the name properties of all of the Tarheel objects, 
//we would still need to access each one individually!

We don't want to change the name of any other TarHeel object to "Rameses"; we only want to change that property for our specific object.

Now that we have the ramster object of type TarHeel and we've changed the values of some of its properties, we can do different things with it. To access an object's properties, we first have to say what object we're talking about, just like we do when we assign values to properties. Here's an example:

import { print } from "introcs";
 
print("Welcome to UNC, " + ramster.name + "!!!");
// prints: Welcome to UNC, Rameses!!

Here, we're reading the name property of the ramster object in our print statement.

Knowing how to access and use properties is nice because properties help give more meaning to the classes and objects we make.