Classes - PowerPoint PPT Presentation

About This Presentation
Title:

Classes

Description:

Classes Variables That Are Not of a Built-in Type Are Objects A Class Is an Object Type A Class Represents a Thing (e.g., employee, wrench, list, store ... – PowerPoint PPT presentation

Number of Views:250
Avg rating:3.0/5.0
Slides: 19
Provided by: Jon1158
Learn more at: http://www.cs.iit.edu
Category:
Tags: classes

less

Transcript and Presenter's Notes

Title: Classes


1
Classes
  • Variables That Are Not of a Built-in Type Are
    Objects
  • A Class Is an Object Type
  • A Class Represents a Thing (e.g., employee,
    wrench, list, store, shopping cart, etc.)
  • Service Class Class used by Other Programs
  • Must Be Created in ClassName.java File

2
Generic Object Declaration
  • class useObject
  • public static void main(String args)
  • Object varName new Object()//instance
  • varName.objectMethod() //invoke method
  • Instance of Object Type Is Called Invoking Object
    of Class Methods
  • varName is an instance of Object
  • varName is invoking object of objectMethod()

3
Creating a Class
  • Programmers Define Classes with
  • Data Members (or Fields, Attributes)
  • Used to store information about each object
  • Each member is a variable of some other type
  • Methods
  • Used to give programmers access to object

4
Designing a Class
  • Decide How It Will Be Used
  • Decide on Interface (i.e., public representation,
    public methods)
  • Decide on Implementation (i.e., private data and
    data manipulation)
  • Example String Methods (interface) includes
    length(), toUpper(), toLower(), charAt(), etc.
  • Example String Implementation includes
    (probably) char variables to hold characters,
    integer to hold length (we dont actually need to
    know)

5
Constructor
  • Class Method of Same Name
  • Example class Employee Method Employee()
  • Called When Variable Declared (instantiated) of
    Class Type
  • Initializes Instantiated Object
  • May Have Multiple Constructors Each with
    Different Parameters

6
Access Modifiers
  • Used to Identify What May Use Methods
  • public any method may use
  • private only methods in same class may use
  • protected only methods in same class or related
    classes may use.

7
Example Class Employee
Employee emp1
String firstName
String lastName
double salary
First and last names and salaries are data
members, or fields or attributes.
8
  • //Employee.java file
  • class Employee
  • private String firstName
  • private String lastName
  • private double salary
  • private final double MAXSALARY 2000000
  • public Employee()
  • // access to invoking object attributes
  • firstName "NoFirstName"
  • lastName "NoLastName"
  • salary 0.0

9
Employee Declaration
Employee emp1 new Employee()
Main Memory (allocated by new, initialized by
Employee() ) emp1
String firstName
"NoFirstName"
String lastName
"NoLasttName"
double salary
0.0
10
Client Program
  • A Program that Uses a Class Is a Client of that
    Class
  • Class Objects are Declared and Instantiated Using
    the new keyword.
  • new ClassName(parameters) Calls Constructor for
    Class
  • Class Public Methods Called Using Dot (e.g.,
    variable.GetName())

11
Client Test Program
  • class useEmployee
  • public static void main(String args)
  • Employee emp1 new Employee()

12
Accessor Methods
  • Public Methods for Getting Attributes of Class

13
  • class Employee
  • private String firstName
  • private String lastName
  • private double salary
  • private final double MAXSALARY 2000000
  • public Employee()
  • firstName "NoFirstName"
  • lastName "NoLastName"
  • salary 0.0
  • public String GetFirstName()
  • return firstName
  • public String GetLastName()
  • return lastName

14
  • class useEmployee
  • public static void main(String args)
  • Employee emp1 new Employee()
  • System.out.println("Name is"
    emp1.GetFirstName() " " emp1.GetLastName())

15
Mutator Methods
  • Class Methods Used to Modify a Class Object are
    Called Mutator Methods
  • Allows Restricted Manipulation of Class Object
    Data

16
  • public boolean SetSalary(double passedSalary)
  • if (passedsalary lt MAXSALARY)
  • salary passedSalary
  • return true
  • else
  • return false

17
  • class useEmployee
  • public static void main(String args)
  • Employee emp1 new Employee()
  • System.out.println("Name is"
    emp1.GetFirstName() " " emp1.GetLastName())
  • if (emp1.SetSalary(55000))
  • System.out.println("Salary set to 55000")
  • else
  • System.out.println("Salary not set")

18
Exam 2
  • EXAM 2 Monday in Lecture!!!
  • 20 of Final Grade
  • Know
  • loops, switch/case
  • Files
  • Input failure (e.g. scan.hasNextInt())
  • Methods
  • Classes
  • Everything Through Lecture Wednesday
Write a Comment
User Comments (0)
About PowerShow.com