classes & objects representin’ real-world things in code-space brian camodeca, mercyhurst...

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

Upload: bertina-bailey

Post on 17-Dec-2015

217 views

Category:

Documents


2 download

TRANSCRIPT

CLASSES & OBJECTSRepresentin’ real-world things in code-spaceBrian 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).

Building a class

Classes have built-in mechanisms for state and behavior

Instance variables represent state Methods represent behaviors Consider this…

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…

A Dog

Name Breed Fur Color Weight Temperament Hungry Tired

Bark Beg Eat Chase Tail Fetch Sleep

State Behavior

A Dog…in code

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

Building a class

For example, Dog.java public class Dog {

String name, breed;

int weight;

public void bark() {

System.out.println(“Woof”);

}

}

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;

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

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);

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

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…

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;

}

}

Encapsulation

But why? Encapsulation! Also known as

“information hiding” Consider this…

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-

“Setters”

Write methods to set instance variables!

public void setWeight (int weight) {

if (weight > 0) {

// phew

this.weight = weight;

}

else {

// AHHHHH! PANIC!!!

// Throw exception

}

}

“Getters”

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

public int getWeight() {

return this.weight;

}

The power of instance variables

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

size and the sound of its bark…

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!”);

}

}

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…

The constructor

public Dog() {// Do Something

}

The constructor

public Dog(String name) {

this.name = name;

}

public Dog(String name, String breed) {

this.name = name;

this.breed = breed;

}

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

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.