Classes & Objects

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

description

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. - PowerPoint PPT Presentation

Transcript of Classes & Objects

Page 1: Classes & Objects

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

Page 2: Classes & Objects

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

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

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

A Dog

Name Breed Fur Color Weight Temperament Hungry Tired

Bark Beg Eat Chase Tail Fetch Sleep

State Behavior

Page 6: Classes & Objects

A Dog…in code Name : String Breed : String Fur Color : String Weight : int Temperament : String Hungry : boolean Tired : boolean

Page 7: Classes & Objects

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

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

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

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

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

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

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

Encapsulation But why? Encapsulation! Also known as

“information hiding” Consider this…

Page 15: Classes & Objects

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

“Setters” Write methods to set instance

variables! public void setWeight (int weight) {

if (weight > 0) {// phewthis.weight = weight;

}else {

// AHHHHH! PANIC!!!// Throw exception

} }

Page 17: Classes & Objects

“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

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

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

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

The constructor public Dog() {

// Do Something }

Page 22: Classes & Objects

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

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

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.