Using ClassesObjects - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Using ClassesObjects

Description:

... using classes is a class. is-a relationships. My desk in Schott 508 is a Desk. My cat named Putty is a Cat. Classes. An object is an instantiation of a class ' ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 17
Provided by: Staf742
Category:

less

Transcript and Presenter's Notes

Title: Using ClassesObjects


1
Using Classes/Objects
  • Chapter 3 part 2
  • Dr. James Jiang

Using Methods, Classes, and Objects
2
Classes
  • Everything is an object
  • Every object is a member of a class
  • A Java program using classes is a class
  • is-a relationships
  • My desk in Schott 508 is a Desk
  • My cat named Putty is a Cat

3
Classes
Reusability
  • An object is an instantiation of a class
  • one tangible example of a class
  • Objects inherit attributes from classes
  • these attributes are predictable as members of
    certain classes
  • reusability
  • Every object that is an instance of a class has
    the methods associated with the class

4
Object-oriented Programming Steps
  • 1. Create the classes of objects from which
    objects will be instantiated
  • 2. Write other classes to use the objects
  • A program (class) that instantiates objects of
    another prewritten class is a class client (class
    user).

5
Creating a Class
  • Create a class header
  • optional access modifier (public, final,
    abstract)
  • public classes are accessible by all objects and
    may be extended (used as a basis for another
    class)
  • keyword class
  • name of the class

6
class Fields
Greater control!
Information hiding
private data public access
  • Include access modifier
  • Example -
  • private int empNum
  • Allowable field modifiers
  • private provides the highest level of security
  • information hiding (private data/public method)

private public protected static final
7
Using Instance Methods
  • Instance methods - methods associated with
    individual objects (dont use static!)
  • Instance methods require an instance of an object!

public int getSiteNumber() return
siteNumber
8
public class EventSite private int
siteNumber public int getSiteNumber() retur
n siteNumber public void setSiteNumber(int
n) siteNumber n
This file CANNOT be run as a program. Why???
9
Declaring Objects
  • Declaring a class does not create an actual
    object
  • To create an object that is an instance of a
    class
  • Supply a type and an identifier
  • Allocate memory for that object

Employee someEmployee someEmployee new
Employee() OR Employee someEmpe new
Employee()
10
Declaring Objects - A Closer Look
Declare an instance of the Employee class
Employee someEmployee someEmployee new
Employee()
Constructor method that constructs an Employee
object
Allocate necessary memory
11
Constructor Methods
  • Method that constructs an object
  • Write your own OR
  • Java writes one for you
  • Employee() - a constructor method written by Java

12
Accessing an Instantiated Object
  • someEmployee.changeSalary(350.00)
  • Method (changeSalary) is applied to object
    (someEmployee) and the argument (350.00) is
    passed to the method

public void changeSalary(double
newAmount) salary newAmount
13
Organizing Classes
  • Place data fields in some logical order at the
    beginning of a class
  • Store class methods in alphabetical order
  • Use comments to document and describe

See Figure 3-29
14
Using Constructors
  • Employee chauffeur new Employee()
  • Constructor methods supplied by Java provide
    specific initial values
  • numeric fields set to 0
  • character fields set to Unicode \u0000
  • boolean fields set to false
  • object type fields set to null

15
Write your own Constructor
  • Must have same name as class it constructs
  • No return type
  • Example

EventSite() siteNumber 999 managerName
ZZZ
16
Exercise 3-6 (p.92)
  • public class Cube
  • public static void main (String args)
  • System.out.println(The cube of 10 is

  • calCube(10.0)
  • public static double calCube (double num)
  • double cube
  • cube num num num
  • return cube
Write a Comment
User Comments (0)
About PowerShow.com