Computer Science II Session 02 - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Computer Science II Session 02

Description:

... we want the instance to have exclusive control over changes to their values. ... For example, I might create a Car class for clients to use. ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 22
Provided by: BenSc
Category:

less

Transcript and Presenter's Notes

Title: Computer Science II Session 02


1
Computer Science IISession 02
  • Cyber-Pets
  • Vocabulary
  • Interpreting the Magic Line

2
The anatomy of an OO program
  • In Java, the main thing you do is write class
    definitions. (R. Morelli)
  • A class definition encapsulates two (three
    actually) things
  • Data (instance variables) (identity and state)
  • Actions (constructors and methods). (behavior)

3
Lets look at a simple example
  • CyberPets are simple creatures that do one of two
    things eat and sleep.
  • CODE original/CyberPet.java

4
The anatomy of an OO program
  • But a class on its own is nearly worthless.
  • The purpose of a class is to serve as a template
    for creating individual objects, or instances, of
    the class.

5
The anatomy of an OO program
  • An object is an instance of a class
  • CyberPet chandler new CyberPet()
  • While a class describes a set of objects that
    behave similarly, each instance of a class is
    distinct and has its own state.
  • CyberPet joey new CyberPet()

6
The anatomy of an OO program
  • Objects interact by sending each other messages.
  • CODE original/CyberPetWorld.java

7
The anatomy of an OO program
  • The world is a class that contains the main()
    function.
  • main() is the big bang that creates one or more
    objects.
  • public class CyberPetWorld
  • public static void main(String args)
  • CyberPet chandler new CyberPet() CyberP
    et joey new CyberPet()

8
Interpreting this magic line
  • What exactly do we mean when we say?
  • public static void main (String args)

9
Interpreting this magic line
  • What exactly do we mean when we say?
  • public static void main (String args)
  • An access modifier.
  • Means that anyone who knows the name of the
    class/method/variable can access to it (more on
    this later)

10
Interpreting this magic line
  • What exactly do we mean when we say?
  • public static void main (String args)
  • Lifetime modifier
  • Means that the named entity is a CLASS
    method/variable (more on this in a couple of
    days, but the quick idea is that the method main
    belongs to the class

11
Interpreting this magic line
  • What exactly do we mean when we say?
  • public static void main (String args)
  • Return type
  • Means that the method will not be returning any
    data

12
Interpreting this magic line
  • What exactly do we mean when we say?
  • public static void main (String args)
  • Method header
  • Contains the name of the method and the required
    parameter (an array of strings referenced by the
    name args) (More on that in a couple of days!)

13
CyberPet is good, but is has some weaknesses
  • We would like to be able to know what an object
    is doing.
  • What would we need to do in order to enable this
    functionality?
  • We cant just check the instance variables,
    because they have private access.

14
Access Modifiers
  • A Java programmer can control access to a class
    and its parts (both data and actions) through the
    use of Access Modifiers.
  • Any item that is declared public can be accessed
    by any piece of code that knows the name of the
    item.
  • Any item that is declared private can be accessed
    only be code within the same class.

15
So when do we use what?
  • Because of this, we normally followed the
    following conventions --
  • Classes are usually declared public, because we
    want to create instances of the class that can
    help us solve a problem.
  • Methods are sometimes declared public, because
    they correspond to messages that other objects
    can send to the instance.
  • Instance variables are private, because we want
    the instance to have exclusive control over
    changes to their values.

16
Access Modifiers
  • But what if want to do things a little
    differently?  Could we have --
  • a private class?
  • Sure.  You might want to declare a private class
    because you dont want just anyone to create
    instances of it. For example, I might create a
    Car class for clients to use. The Car might
    contain instances of a wheel class, but clients
    should not be able to create their own instances.
  • a private method?
  • You might want to declare a private method,
    because it is not a part of the objects public
    interface. Perhaps it is simply a helper to one
    of the public methods. We may want to decompose
    the fixWheel method in Car into several helper
    methods.
  • a public instance variable?
  • You might want to declare a public instance
    variable simply for your convenience as a
    programmer. BUT..

17
CyberPet is good, but is has some weaknesses
  • We would like to be able to know what an object
    is doing.
  • What would we need to do in order to enable this
    functionality?
  • We cant just check, because that information is
    private.
  • Therefore, we need to add an accessor method.

18
Accessor Method
  • A public method which provides one instance with
    information about a second instance.

19
Accessor Method
  • Sometimes these provide direct access to the
    value of a variable
  • public boolean getIsEating()
  • return isEating
  • public boolean getIsSleeping()
  • return isSleeping

20
Accessor Method
  • But in many cases it will be better to remember
    the old adage
  • Ask not what you can do for your class
  • but what your class can do for you.
  • (my apologies to JFK).

21
Accessor Method
  • public void printStatus()
  • if (isEating)
  • System.out.println(I am eating)
  • else
  • System.out.println(I am sleeping)
  • CODE status/CyberPet.java
Write a Comment
User Comments (0)
About PowerShow.com