You are on page 1of 11

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.
9

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
• 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.