P. 1
Lecture 21b

Lecture 21b

|Views: 11|Likes:
Published by api-26783388

More info:

Published by: api-26783388 on Oct 19, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

1

8
Classes and Objects: A Deeper Look
 2005 Pearson Education, Inc. All rights reserved.

2

OBJECTIVES
In this chapter you will learn: Encapsulation and data hiding. The notions of data abstraction and abstract data types (ADTs). To use keyword this. To use static variables and methods. To import static members of a class. To use the enum type to create sets of constants with unique identifiers. How to declare enum constants with parameters.

 2005 Pearson Education, Inc. All rights reserved.

3

8.2 Time Class Case Study
•public services (or public interface)
– public methods available for a client to use

• If a class does not define a constructor the compiler will provide a default constructor • Instance variables
– Can be initialized when they are declared or in a constructor – Should maintain consistent (valid) values

IT114

Winter 2007

R.B. Lenin

DA-IICT

 2005 Pearson Education, Inc. All rights reserved.

4

IT114

Winter 2007

R.B. Lenin

DA-IICT

 2005 Pearson Education, Inc. All rights reserved.

5

8.2 Time Class Case Study (Cont.)
•String method format
– Similar to printf except it returns a formatted string instead of displaying it in a command window

•new implicitly invokes Time1’s default constructor since Time1 does not declare any constructors

IT114

Winter 2007

R.B. Lenin

DA-IICT

 2005 Pearson Education, Inc. All rights reserved.

6

IT114

Winter 2007

R.B. Lenin

DA-IICT

 2005 Pearson Education, Inc. All rights reserved.

7

IT114

Winter 2007

R.B. Lenin

DA-IICT

 2005 Pearson Education, Inc. All rights reserved.

8

IT114

Winter 2007

R.B. Lenin

DA-IICT

 2005 Pearson Education, Inc. All rights reserved.

8.4 Referring to the Current Object’s Members with the this Reference
• The this reference
– Non-static methods implicitly use this when referring to the object’s instance variables and other methods – Can be used to access instance variables when they are shadowed by local variables or method parameters

9

• A .java file can contain more than one class
– But only one class in each .java file can be public

IT114

Winter 2007

R.B. Lenin

DA-IICT

 2005 Pearson Education, Inc. All rights reserved.

10

IT114

Winter 2007

R.B. Lenin

DA-IICT

 2005 Pearson Education, Inc. All rights reserved.

11

IT114

Winter 2007

R.B. Lenin

DA-IICT

 2005 Pearson Education, Inc. All rights reserved.

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->