classes & objects

Download Classes & Objects

Post on 23-Feb-2016

30 views

Category:

Documents

0 download

Embed Size (px)

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

Classes & Objects

Classes & ObjectsRepresentin real-world things in code-spaceBrian Camodeca, Mercyhurst CollegeWhat 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 classClasses have built-in mechanisms for state and behaviorInstance variables represent stateMethods represent behaviorsConsider thisRelease the houndsWhat if our application needs to work with dogs?Built-in types like integers, strings, and arrays alone cant really help us here.But, maybe we can unify them in this new class thingie!Consider things about a dog

A DogNameBreedFur ColorWeightTemperamentHungryTiredBarkBegEatChase TailFetchSleep

StateBehaviorA Dogin codeName : StringBreed : StringFur Color : StringWeight : intTemperament : StringHungry : booleanTired : booleanBuilding a classFor example, Dog.javapublic class Dog {String name, breed;int weight;public void bark() {System.out.println(Woof);} }

Building a classCreating a class creates a new typeIf we name our class Dog, we can now create variables of type Dog Dog fido;Creating an objectAn object is a given instance of a classA class is the abstract idea, an object is the concrete exampleDog fido = new Dog();

fido is the object, an instance of the Dog classManipulating the objectWe 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 objectWe can invoke behaviors of the object by calling its methods by nameFor example,someDog.bark();Prints Woof! to the consoleManipulating the object IIBefore, we changed the values directly.DONT ALLOW THISSet instance variables as private and create public getter and setter methods For exampleManipulating the object IIpublic class Dog {private String name, breed;private int weight;public void setName(String name) {this.name = name;}public String getName() {return this.name;}

}

EncapsulationBut why?Encapsulation! Also known as information hidingConsider thisEncapsulation(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-

SettersWrite methods to set instance variables!public void setWeight (int weight) {if (weight > 0) {// phewthis.weight = weight;}else {// AHHHHH! PANIC!!!// Throw exception} }GettersUh-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 variablesMake the class dynamic!Consider the correlation between a dogs size and the sound of its bark

The power of instance variablespublic 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 constructorSpecial function that is invoked upon object instantiationJava convention: named the same as the class name, and is that class return typeFor exampleThe constructorpublic Dog() {// Do Something }The constructorpublic Dog(String name) {this.name = name; }

public Dog(String name, String breed) {this.name = name;this.breed = breed; }The toString() methodA method that returns a string representation of that object.By default its not very helpful, but we can implement our own toString() method!@OverrideWhere 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.