Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CLASSES & OBJECTS Representin’ real-world things in code-space Brian Camodeca, Mercyhurst College."— Presentation transcript:

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

2 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).

3 Building a class  Classes have built-in mechanisms for state and behavior  Instance variables represent state  Methods represent behaviors  Consider this…

4 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…

5 A Dog  Name  Breed  Fur Color  Weight  Temperament  Hungry  Tired  Bark  Beg  Eat  Chase Tail  Fetch  Sleep StateBehavior

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

7 Building a class  For example, Dog.java  public class Dog { String name, breed; int weight; public void bark() { System.out.println(“Woof”); }

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

9 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

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

11 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

12 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…

13 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; }

14 Encapsulation  But why?  Encapsulation! Also known as “information hiding”  Consider this…

15 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-

16 “Setters”  Write methods to set instance variables!  public void setWeight (int weight) { if (weight > 0) { // phew this.weight = weight; } else { // AHHHHH! PANIC!!! // Throw exception }

17 “Getters”  Uh-oh, only other members of the class can “see” private members. We need a liaison!  public int getWeight() { return this.weight; }

18 The power of instance variables  Make the class dynamic!  Consider the correlation between a dog’s size and the sound of its bark…

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

20 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…

21 The constructor  public Dog() { // Do Something }

22 The constructor  public Dog(String name) { this.name = name; }  public Dog(String name, String breed) { this.name = name; this.breed = breed; }

23 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

24 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.


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

Similar presentations


Ads by Google