Chapter 10 Classes and Methods IV: Static Methods and Variables - PowerPoint PPT Presentation

About This Presentation
Title:

Chapter 10 Classes and Methods IV: Static Methods and Variables

Description:

An Introduction to Computer Science Using Java (2nd Edition) by ... Classes that contain concrete definitions of the abstract interface methods ... – PowerPoint PPT presentation

Number of Views:69
Avg rating:3.0/5.0
Slides: 15
Provided by: brucem151
Category:

less

Transcript and Presenter's Notes

Title: Chapter 10 Classes and Methods IV: Static Methods and Variables


1
Chapter 10Classes and Methods IVStatic 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
  • introduce class variables and class methods
  • class variables have only one instance and are
    not contained in any object
  • class methods have no receiver and can only
    access class variables
  • discuss overloading for class methods
  • introduce Java interfaces which specify object
    behavior
  • discuss the use of methods to modularize large
    programs
  • illustrate process of stepwise program refinement

3
Class Variables
  • Associated with a class, not class instance
  • Only one instance of each class variable, not one
    instance per object
  • Created by adding the modifier static to the
    variables declaration
  • Allows variables to be modified once for all
    class objects
  • Can be accessed by class methods

4
(No Transcript)
5
Class Methods
  • Methods declared with static modifier
  • Can be defined in any class
  • Not associated with an object
  • Is not invoked by sending it as a message to an
    object
  • The class method f in the class C is invoked
    using the notation f.C( )
  • It has no receiver and cannot refer to instance
    variables
  • It can refer to and manipulate class variables

6
Classes with No Instance Variables or Methods
  • Some classes only contain class variables and
    methods
  • Since these classes have no instance variables
    they need no constructors
  • Provide a convenient way of packaging collections
    of useful methods
  • Some collections like Math
  • define symbolic constants that are unchangable
  • public static final double E
  • public static final double PI
  • methods are all declared to be public and static
  • these methods operate only on built-in data types

7
Classes without Instance Methods
  • Class ClassnameClient
  • // Author, date, expalnation
  • public static void main (String args
  • Classname variable new Classname( )
  • variable, method( )

8
Sformat Class
  • Purely static class taken from CSLib
  • Contains a number of overloaded methods
  • Overloaded methods
  • share the same name
  • differ in their argument number or type
  • sprintr is an example of an overloaded method in
    Sformat

9
Modular Development
  • Top-down approach
  • problem is viewed from the top and written in
    outline form
  • the outline is refined by looking at each section
    in greater detail
  • Stepwise Refinement
  • the process of adding detail to successive
    sections until the constituent parts become
    apparent and can be written
  • the debugging needs of each component dictate the
    order in which they are written

10
Interfaces
  • Declared like classes using the word interface
    instead of class
  • Must contain method declarations without bodies
  • May contain symbolic constants
  • UML equivalent of Java interface is indicated by
    an italicized class name and a dashed line
  • Example
  • interface interface_name
  • definitions of symbollic constants and
    declarations of abstract methods

11
(No Transcript)
12
Implementing Interface Methods
  • Classes that contain concrete definitions of the
    abstract interface methods indicate this in their
    header declarations
  • This serves as a contract whereby the class
    declares it intends to implement every method
    form the interface
  • Example
  • public class classname implements interface-name

13
Implementing Multiple Interfaces
  • Classes can implement more than one interface
  • Example
  • class C implements I1, I2
  • Like class definitions interface definitions are
    stored in files have the .java extension
  • Packages can contain both interfaces and classes

14
Defining Uniform Constants
  • Interfaces can be used ro give definitions of
    symbolic constants used in several classes
  • Makes it easier to keep symbolic constants in
    sync between classes
  • Example
  • public interface Direction
  • int NORTH0, EAST1, SOUTH3, WEST3
Write a Comment
User Comments (0)
About PowerShow.com