Chapter 7 Classes and Methods III: Static Methods and Variables - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Chapter 7 Classes and Methods III: Static Methods and Variables

Description:

describe the class interface and how it responds to each client message ... can be overloaded (more than one definition in the same class) Clock Constructor ... – PowerPoint PPT presentation

Number of Views:101
Avg rating:3.0/5.0
Slides: 27
Provided by: brucem94
Category:

less

Transcript and Presenter's Notes

Title: Chapter 7 Classes and Methods III: Static Methods and Variables


1
Chapter 7Classes and Methods IIIStatic Methods
and Variables
  • Lecture Slides to Accompany
  • An Introduction to Computer Science Using Java
    (2nd Edition)
  • by
  • S.N. Kamin, D. Mickunas, E. Reingold

2
Chapter Preview
  • In this chapter we will
  • describe user-defined classes
  • instance variables
  • constructors
  • instance methods
  • present several examples of classes
  • discuss the concepts of mutability and visibility
  • describe method overloading

3
Object-Oriented Programming
  • OOP supports the view that programs are composed
    of interacting objects
  • Objects are composed of
  • values known as attributes or instance variables
  • operations (actions) that can be performed on
    these values know as instance methods
  • Messages requesting an action or value are sent
    to objects
  • Objects respond to messages that are in their
    protocols or interfaces

4
Objects
  • Encapsulate data values within a single entity
  • Their behavior is often general enough to allow
    reuse in a variety of situations
  • Often form a basis from which other objects can
    be derived using a mechanism known as inheritance
  • Are a type container that is stored on the system
    heap
  • A client is program that uses an object

5
(No Transcript)
6
Client Rights
  • To declare variables of the class type
  • To create instances of the class using
    constructors
  • To send messages to instances of the class by
    invoking class instance methods
  • To know the class public interface
  • instance method names
  • parameter number and types
  • return types
  • To know the instance methods that alter (mutate)
    the instance

7
Class Rights
  • To define the class public interface
  • To hide all the implementation details from the
    client
  • To protect internal data from client access
  • To change implementation details at any time,
    provided the public interface remains intact
  • To change the public interface with client
    concurrence

8
Revised Class Definition
  • public class name
  • declarations of instance variables
  • constructor definitions
  • method defintions
  • Every class needs one or more constructor
    definitions

9
Revised Class Definition
  • Instance variables
  • local data contained in class
  • Method definitions
  • describe the class interface and how it responds
    to each client message
  • Constructors definitions
  • describe how to initialize instance variables in
    a new object

10
Constructors
  • Look like regular instance methods
  • Never have a return type
  • Always have the same name as the class name
  • May have parameters
  • Default constructors have no parameters
  • Constructors can be overloaded (more than one
    definition in the same class)

11
Clock Constructor
  • public class Clock
  • int hour, minute
  • public Clock ()
  • hour 12
  • minute 0
  • public Clock (int h, int m)
  • hour h
  • minute m

12
Using Constructors
  • Clock c1 new Clock( ) // c1 set to 1200
  • Clock c2 new Clock(8, 20) // c2 set to 820
  • Clock c3 new Clock() // c3 set to 820
  • c3.setHour(8)
  • C3/setMinute(20)

13
Overloading Methods
  • Methods can be overloaded
  • Overloaded methods have the same name but the
    versions have differ in the numbers or types of
    parameters
  • Overloading allows methods with the name name to
    have different return types

14
Methods Calling Other Methods
  • Methods are allowed to call other methods in the
    same class without specifying an explicit
    receiver
  • This allows overloaded methods to call one
    another without repeating redundant code
  • Example
  • public void display (DrawingBox d, int r)
  • display(d, d.getDrawableWidth()/2,
  • d.getDrawableHeight()/2, r)

15
Dot Notation
  • We can also use dot notation to view instance
    variables of the same class that are different
    from the receiver
  • Example
  • public boolean priorTo (Clock c)
  • return (hour lt c.hour
  • hour c.hour
  • minute lt c.minute)

16
this
  • Can be used by any object to refer to itself in
    any class method
  • Typically used to
  • Avoid variable name collisions
  • Pass the receiver as an argument
  • Chain constructors

17
Avoiding Variable Name Collisions
  • public void set (int hour, int minute)
  • int totalMinutes (hour 60 minute)
  • if (totalMinutes lt 0)
  • totalMinutes totalMinutes (12 60)
  • this.hour totalMinutes / 60
  • if (this.hour 0)
  • this.hour 12
  • this.minute totalMinute 60

18
Passing Receiver as Argument
  • public boolean after (clock c)
  • return c.priorTo(this)

19
Chaining Constructors
  • public Clock ()
  • // calls constructor below
  • this(12, 0)
  • public Clock(int hour, int minute)
  • set(hour, minute)

20
Visibility Qualifiers
  • public int x // client creating instance o of
    this
  • // class can access x by writing
    o.x
  • private int y // no can access y directly,
    access
  • // provided though class methods
  • To enforce complete information hiding all
    instance variables should be declared using
    private
  • The default visibility of instance variables lies
    between private and public (explained later in
    the text)

21
Mutation
  • An object can be changed by assigning a new value
    to one or more of its instance variables
  • Example
  • d new DrawingBox()
  • c new Clock()
  • c.set(10, 20)
  • c.display(d, 50, 50, 50)
  • c.set(5, 40)

22
(No Transcript)
23
Mutability
  • Transforming an object from one state to another
  • Only objects can be mutated, primitive values are
    not (e.g. x4 does not change x)
  • Objects are only mutable if its interface
    includes mutating methods

24
Representation Independence
  • In OOP the representation of data is
    encapsulated, this means that the data
    representation may be changed without affecting
    the previous class behavior
  • Clients should not be able to detect that a
    change has occurred
  • Sometimes this is know as implementation
    independence

25
main
  • Every Java program needs a class containig a
    static method called main
  • Static methods (or class methods) are special
    methods that do not require receivers
  • Static methods cannot refer to class instance
    variables, but can be invoked when no class
    instances exist
  • Static methods will be discussed in more detail
    in Chapter 10

26
Nonmutation
  • Creating a new object similar to original, but
    including the desired change
  • Example
  • public Clock set_nonmut
  • (int hour, int Minute)
  • return new Clock(hour, minute)
Write a Comment
User Comments (0)
About PowerShow.com