Building Java Classes - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Building Java Classes

Description:

Cloning Classes. The ability to copy an object is needed ... Temperature t2 = (Temperature) t1.clone ... copy = (Temperature) super.clone ( ); return copy; ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 25
Provided by: calpoly
Category:

less

Transcript and Presenter's Notes

Title: Building Java Classes


1
Building Java Classes
  • Class Definition
  • Standard Methods
  • Static Variables and Methods
  • Robert Stumpf, Ph.D.
  • CIS Department

2
Class Definition
  • A Java class is simply specified by
  • class ltnamegt
  • private lttypegt ltnamegt // attribute
  • public lttypegt ltname ( ) // method

3
Class Definition
  • A Java class example
  • class Temperature
  • private int celsius // attributepublic
    int getCelsius ( ) // accessor
  • return celsius
  • public void setCelsius (aCelsius)
    // mutator celsius aCelsius

4
Class Definition
  • Getters are called accessors
  • public int getCelsius ( )
  • return celsius
  • Setters are called mutators
  • public void setCelsius (aCelsius)
  • celsius aCelsius

5
Class Definition
  • Naming of arguments
  • Your instructor uses the a or an before the
    name
  • This facilitates ease of reading code
  • Resolves conflicts when local variables might
    have the same name
  • For example aCelsius and celsius
  • The default for IBM Visual Age is acceptable
    (newValue)

6
Class Definition
  • Testing the temperature example class
  • int celsius // declaration
  • Temperature temperature // declaration
  • temperature new Temperature ( ) //
    instantiation
  • temperature.setCelsius (20) // populate
  • celsius temperature.getCelsius ( ) //
    retrieve
  • Note a default constructor exists called
  • Temperature ( )

7
Standard Methods
  • In addition to the default constructor one may
    create additional constructors
  • Constructors allow one to instantiate and
    populate a class in one statement rather that
    having to perform mutations to the attributes
    after instantiation
  • One can then instantiate and populate a new
    temperature without having to use a mutator

8
Standard Methods
  • A Java constructor example (with no parameters)
  • private int celsius
  • Temperature ( )
  • celsius 0
  • Constructor has the same name as the class

9
Standard Methods
  • A Java constructor example (with parameters)
  • private int celsius
  • Temperature ( int aCelsuis)
  • celsius aCelsuis
  • This constructor is more convenient as it both
    instantiates and populates

10
Standard Methods
  • It is standard procedure to build a constructor
    for each class that populates all of its instance
    variables
  • If a class has ten instance variables the
    constructor will then have ten arguments
  • The exception to this is date instance variables
    which are often set to today's date from the
    system

11
Standard Methods
  • In addition to constructors there is a toString
  • A toString allows one to obtain a printable copy
    of the object for display purposes
  • It is standard to always include a toString
  • The inherited toString delivers a cryptic string
    so the designer must write their own

12
Standard Methods
  • A toString converts object to a string
  • class Temperature
  • private int celsius
  • public String toString ( )
  • return Integer.toString (celsius) " o
    C"
  • Returns "20 0 C" if value was 20

13
Standard Methods
  • A toString that includes the class name
  • class Temperature
  • private int celsius
  • public String toString ( )
  • return this.getClass ( )
    Integer.toString (celsius)
  • Returns "package.Temperature 20"

14
Standard Methods
  • Testing the Java toString exampleString string
  • Temperature temperature
  • temperature new Temperature (20)
  • string temperature.toString ( )
  • System.out.println(string)
  • System.out.println(temperature) // Gives the
    same answer
  • Note that either using the toString explicitly or
    implicitly works equally well

15
Static Variables Methods
  • A Java static (class) variable example
  • class Temperature
  • private int celsius
  • private static int instanceCount
  • Temperature (int aCelsius)
  • celsius aCelsius
  • instanceCount

16
Static Variables Methods
  • A Java static (class) method example
  • public static int getInstanceCount ( )
  • return instanceCount
  • Note that there is only one value of
    instanceCount per class even if there are many
    instances
  • Note the word static in method declaration

17
Static Variables Methods
  • Testing the Java static variable example
  • int celsius, numberInstances
  • Temperature temperature
  • temperature new Temperature (20)
  • celsius temperature.getCelsius ( )
  • numberInstances Temperature.getInstanceCount
    ( )
  • Note that the static method is sent to the class
    name

18
Cloning Classes
  • The ability to copy an object is needed
  • This is because Java uses pointers to objects
  • For example
  • Temperature t1 new Temperature (20)
  • Temperature t2 t1
  • This results in only one object and both
    variables are referencing it

19
Cloning Classes
  • What needs to be done is
  • Temperature t1 new Temperature (20)
  • Temperature t2 (Temperature) t1.clone ( )
  • (Temperature) is a cast (a conversion from an
    object to Temperature)
  • clone ( ) method must be provided by the
    programmer

20
Cloning Classes
  • A typical clone method
  • public Object clone ( ) throws
    CloneNotSupportedException
  • Temperature copy
  • copy (Temperature) super.clone ( )
  • return copy
  • Note that this method overrides the parents
    method

21
Cloning Classes
  • Notes on cloning
  • The actual method should allow for exceptions
    using try and catch
  • This, however, is a later topic
  • Note that super refers to parent class of Object

22
Summary
  • Classes are needed for model objects
  • Classes can also be used to extend GUI classes
  • Or one may develop or extend the java library
  • An example of this might be to build a better
    toString for GregorianCalendar

23
Summary
  • Occasionally static variables and methods are
    needed for such things as counters
  • Standard methods are expected in your business
    object model
  • Constructors
  • Accessors and Mutators
  • toString ( )
  • clone ( ) // Sometimes

24
Thank You
  • A little in the right direction
  • can make a big difference.Thank you
  • Robert Stumpf
  • email rvstumpf_at_csupomona.edu
  • url http//www.csupomona.edu/rvstumpf
Write a Comment
User Comments (0)
About PowerShow.com