Top Banner
CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College
24

CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Dec 17, 2015

Download

Documents

Bertina Bailey
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

CLASSES & OBJECTSRepresentin’ real-world things in code-spaceBrian Camodeca, Mercyhurst College

Page 2: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

What is a class?

Defines, conceptually, some real-world thing and how the computer can build such a thing; a blueprint.

Consists of state (stuff describing the thing) and behavior (stuff the thing does).

Page 3: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Building a class

Classes have built-in mechanisms for state and behavior

Instance variables represent state Methods represent behaviors Consider this…

Page 4: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Release the hounds

What if our application needs to work with dogs?

Built-in types like integers, strings, and arrays alone can’t really help us here.

But, maybe we can unify them in this new class thingie!

Consider things about a dog…

Page 5: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

A Dog

Name Breed Fur Color Weight Temperament Hungry Tired

Bark Beg Eat Chase Tail Fetch Sleep

State Behavior

Page 6: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

A Dog…in code

Name : String Breed : String Fur Color : String Weight : int Temperament : String Hungry : boolean Tired : boolean

Page 7: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Building a class

For example, Dog.java public class Dog {

String name, breed;

int weight;

public void bark() {

System.out.println(“Woof”);

}

}

Page 8: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Building a class

Creating a class creates a new type If we name our class “Dog”, we can now

create variables of type Dog Dog fido;

Page 9: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Creating an object

An object is a given instance of a class A class is the abstract idea, an object is

the concrete example Dog fido = new Dog();

“fido” is the object, an instance of the Dog class

Page 10: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Manipulating the object

We can change the state of the object by manipulating its instance variables directly (for now)

For example, Dog someDog = new Dog(); someDog.name =

“Fido”; someDog.weight = 35; System.out.println(someDog.name);

Page 11: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Using the object

We can invoke behaviors of the object by calling its methods by name

For example, someDog.bark(); Prints “Woof!” to the console

Page 12: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Manipulating the object II

Before, we changed the values directly. DON’T ALLOW THIS Set instance variables as “private” and

create public “getter” and “setter” methods

For example…

Page 13: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Manipulating the object II

public class Dog {

private String name, breed;

private int weight;

public void setName(String name) {this.name = name;

}

public String getName() {

return this.name;

}

}

Page 14: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Encapsulation

But why? Encapsulation! Also known as

“information hiding” Consider this…

Page 15: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Encapsulation

(Assuming variables are still public) Dog someDog = new Dog(); someDog.name =

“Fido”; someDog.weight = -7; No control over what values get assigned to the instance

variables. If only we had a way to make sure-

Page 16: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

“Setters”

Write methods to set instance variables!

public void setWeight (int weight) {

if (weight > 0) {

// phew

this.weight = weight;

}

else {

// AHHHHH! PANIC!!!

// Throw exception

}

}

Page 17: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

“Getters”

Uh-oh, only other members of the class can “see” private members. We need a liaison!

public int getWeight() {

return this.weight;

}

Page 18: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

The power of instance variables

Make the class dynamic! Consider the correlation between a dog’s

size and the sound of its bark…

Page 19: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

The power of instance variables

public void bark() {

if (this.weight > 50) {

System.out.println(“Woof!”);

}

else if (this.weight > 15) {

System.out.println(“Ruff!”);

}

else {

System.out.println(“Yip!”);

}

}

Page 20: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

The constructor

Special function that is invoked upon object instantiation

Java convention: named the same as the class’ name, and is that class’ return type

For example…

Page 21: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

The constructor

public Dog() {// Do Something

}

Page 22: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

The constructor

public Dog(String name) {

this.name = name;

}

public Dog(String name, String breed) {

this.name = name;

this.breed = breed;

}

Page 23: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

The toString() method

A method that returns a string representation of that object.

By default it’s not very helpful, but we can implement our own toString() method!

@Override

Page 24: CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College.

Where is “static”?

Static members of a class can be accessed without an instantiation, like our readLine() method.

All our variables pertain to the particular instance of the dog, therefore they are non-static.