Object-Oriented Programming

Douglas Blank, Bryn Mawr College, CS206, Spring 2017

Topics:

  • classes
  • instances
  • methods
  • fields/properties

Objects

An object is a new type of thing, like String, int, or float.

You define a new type of object using the class keyword.

You create an instance of an object using the new keyword.

The simplest kind of class:

In [1]:
class Dog {

}
|  Added class Dog

In [2]:
Dog dog1 = new Dog();
|  Added variable dog1 of type Dog with initial value Dog@44e81672

In [3]:
Dog dog2;
|  Added variable dog2 of type Dog

In [4]:
dog2 = dog1;
|  Variable dog2 has been assigned the value Dog@44e81672

A bit more complicated:

In [5]:
class Dog {
    String name;
    
    Dog(String name) { // constructor
        this.name = name;
    }
    
    void speak() {
        printf("woof, woof! My name is %s", this.name);
    }
}
|  Replaced class Dog
|      constructor Dog in class Dog cannot be applied to given types;
|        required: java.lang.String
|        found: no arguments
|        reason: actual and formal argument lists differ in length
|      Dog dog1 = new Dog();
|                 ^-------^
|    Update replaced variable dog2, reset to null
|    Update overwrote class Dog

Making an instance:

In [6]:
Dog dog1 = new Dog("Snoopy");
|  Replaced variable dog1 of type Dog with initial value Dog@6f79caec
|      constructor Dog in class Dog cannot be applied to given types;
|        required: java.lang.String
|        found: no arguments
|        reason: actual and formal argument lists differ in length
|      Dog dog1 = new Dog();
|                 ^-------^

In [7]:
dog1.name
|  Expression value is: "Snoopy"
|    assigned to temporary variable $7 of type String

Out[7]:
Snoopy

Making a bunch of instances:

In [8]:
Dog dog2 = new Dog("Gracie");
Dog dog3 = new Dog("Louis");
Dog dog4 = new Dog("Mr. Puddles");
Dog dog5 = new Dog("Kirena");
Dog dog6 = new Dog("Pepper");
Dog dog7 = new Dog("Woofie");
Dog dog8 = new Dog("Boo");
|  Modified variable dog2 of type Dog with initial value Dog@5d3411d

|  Added variable dog3 of type Dog with initial value Dog@5fe5c6f

|  Added variable dog4 of type Dog with initial value Dog@763d9750

|  Added variable dog5 of type Dog with initial value Dog@2be94b0f

|  Added variable dog6 of type Dog with initial value Dog@17ed40e0

|  Added variable dog7 of type Dog with initial value Dog@31b7dea0

|  Added variable dog8 of type Dog with initial value Dog@47d384ee

In [9]:
dog1.speak();
dog2.speak();
dog3.speak();
dog4.speak();
dog5.speak();
dog6.speak();
dog7.speak();
dog8.speak();
woof, woof! My name is Snoopy
woof, woof! My name is Gracie
woof, woof! My name is Louis
woof, woof! My name is Mr. Puddles
woof, woof! My name is Kirena
woof, woof! My name is Pepper
woof, woof! My name is Woofie
woof, woof! My name is Boo

Another new type:

In [ ]:
class Student {
    String name;

    Student(String name) {
        this.name = name;
    }
}
In [ ]:
Student student1 = new Student("Kevin");
In [ ]:
Student student2 = new Student("Taylor");

Expressions

In [ ]:
student1.name == student2.name

Data Structures

Imagine a chain link:

We will use this idea to create a "linked list"

Linked List

  • starts out with no items (we'll call them nodes)
  • we add new links/nodes onto the end
In [10]:
class Node {
    String name;
    Node next;
    
    Node(String name) {
        this.name = name;
    }
}
|  Added class Node

In [11]:
Node node = new Node("Kevin");
|  Added variable node of type Node with initial value Node@483bf400

In [12]:
node.next = new Node("Elizabeth");
|  Expression value is: Node@77f03bb1
|    assigned to temporary variable $25 of type Node

Out[12]:
Node@77f03bb1
In [13]:
node.name
|  Expression value is: "Kevin"
|    assigned to temporary variable $26 of type String

Out[13]:
Kevin
In [14]:
node.next.name
|  Expression value is: "Elizabeth"
|    assigned to temporary variable $27 of type String

Out[14]:
Elizabeth
In [15]:
node.next.next = new Node("Chad");
|  Expression value is: null
|    assigned to temporary variable $28 of type Node

Out[15]:
null
In [ ]:
class Node {
    String name;
    Node next;
    
    Node(String name) {
        this.name = name;
    }
    
    void append(Node new_node) {
        if (next == null) {
            this.next = new_node;
        } else {
            this.next.append(new_node);
        }
    }
}
In [ ]:
node = new Node("a");
In [ ]:
node.append(new Node("b"));
In [ ]:
node.name
In [ ]:
node.next.name
In [ ]:
class Node {
    String name;
    Node next;
    
    Node(String name) {
        this.name = name;
    }
    
    void append(Node new_node) {
        if (next == null) {
            this.next = new_node;
        } else {
            this.next.append(new_node);
        }
    }
    
    void print() {
        System.out.print(name + " ");
        if (this.next != null) {
            this.next.print();
        }
    }
}
In [ ]:
node = new Node("a");
In [ ]:
node.append(new Node("b"));
In [ ]:
node.print();