Objects: Creating your own data types

What is a class?

Classes allow us to create complex data types, using primitive types


A representation of a Student

Syntax of a class

Use the class keyword

The public keyword, let's us access the properties of Student from outside the class definition


Declaring variable of type Student

s1 is an instance of the Student class

Objects are reference types


Memory Address Variable Type Variable name Value
@1001 Student s1 @1100
... ... ... ...
@1100 int id 0
@1101 String first_name ""
@1102 String last_name ""
@1103 String major_program ""

We are reserving new memory space for s1

Accessing properties of a class

Put a . after the variable name


Null references

You can make a reference type point to nowhere in your memory


I.e. you can declare a reference type without the new statement


Memory Address Variable Type Variable name Value
@1001 Student s1 null
@1001 int[] array null

No new memory gets reserved for s1 or array

Using a class in Arrays

We can now use Student as any other type



Declaring an array of elements of type Student


Each element in comp202_students points an instance of the Student class, in the computer's memory

  • Each position in the array is null by default
  • We need to initialize each position in the array before using it

The constructor method

Defines some default code that is executed when we create a variable of our class


Declaring variable of type Student calls the constructor method of Student

s1 is an instance of the Student class

What are class methods?

We can execute code that depends on the properties of an object


A representation of a Student

What are class methods?

We can execute code that depends on the properties of an object


A representation of a Student

Class method syntax

Remove the keyword static, from the method declaration

Class method syntax

Remove the keyword static, from the method declaration

An object instance can access its class attributes inside a class method

Method overloading

Method overloading

Declaring multiple class methods with the same name and return type

Must have different input arguments


E.g multiple constructors

Method overloading

Declaring multiple class methods with the same name and return type

Must have different input arguments


E.g multiple constructors

Method overloading

Declaring multiple class methods with the same name and return type

Must have different input arguments


E.g multiple constructors

An example - Creating a Course class

  • The Course class
    • A Course has an course name, a course number, and an instructor name
    • A Course has a list of registered students
    • Write a method that assigns to each student a random major_program from { "B.A.", "B.Eng.", "B.Sc.", "B.Comm.", "M.Sc", "Ph.D" }
    • Write a method that counts how many students are enrolled in each program

A simple exercise - Creating a 2D Point Class

A harder exercise - Creating a 2D Point Class, and using in a Polygon class

Resources

/