Classes and Class
Members
Chapter 3
3
Public Interface
Contract between class and its clients
to fulfill certain responsibilities
The client is an object that calls methods
on another object or class.
The interface details what the class does,
but not how it does it.
3
Private Implementation
The private implementation of a class
is the detailed explanation of how the
class does its work.
Clients do not know these details.
3
Encapsulation
A good contract promises a well-
defined area of responsibility.
Each class should fully encapsulate
(contain) all its responsibilities.
What the class does should be clear but
the details of how it does it should be
hidden.
3
What is data hiding?
Data hiding is a term used to indicate
that a class’s internal state is hidden
from its clients.
3
Delegation
A class should fulfill and restrict itself
to one area of responsibility.
Classes should delegate outside
responsibilities to other classes.
3
Class Fields
Class fields hold the class data. Each
field has a type:
Intrinsic Types
The Java language defines eight intrinsic types
Java Library Types
User-Defined Types
Classes defined by the programmer
3
Public vs. Private Fields
The keywords public and private
are referred to as access modifiers.
Use private keyword to hide field from
clients.
Use public keyword to allow clients
access to field.
3
Methods
Methods define the behavior of the
class.
Methods can return a value:
Return type can be an intrinsic type or an object.
Use the keyword void if a value is not to be returned by
the method.
Methods can accept parameters:
A parameter is an object you pass in to the method
when you call it.
Parameters follow the same naming conventions as
field names.
3
Accessorand MutatorMethods
Accessor methods return information
Typically named to indicate that a value is being
returned:
getAge()
getAccountBalance()
Mutator methods modify the state of
the object.
Typically named to indicate that a value is being
changed:
setAge()
addDeposit()
3
Why Use Accessors and Mutators?
Follows the concept of data hiding
Allows the class designer to change how
a method is implemented without
rewriting clients
3
What is a constructor?
A constructor is a special method that
creates an instance of your class.
3
Constructors
Constructors initialize objects with
valid values.
Constructors have special rules:
May accept parameters
Never marked with a return type
A default constructor:
Takes no parameters
Is provided by the compiler if you do not
provide one
3
Static Members
Static members belong to the class
and are shared by all instances of the
class.
Declared using the static keyword
Can be accessed without an instance of
the class
3
Dot Operator
The dot operator indicates that a
method or member field (right side of
dot) belongs to the object or class (left
side of dot).
ClassName.someStaticMemberField
ObjectName.someMethod()
3
The this Keyword
Is a self-reference to the current object
Provided only in instance methods, not in
static methods

Chapter 03

  • 1.
  • 2.
    3 Public Interface Contract betweenclass and its clients to fulfill certain responsibilities The client is an object that calls methods on another object or class. The interface details what the class does, but not how it does it.
  • 3.
    3 Private Implementation The privateimplementation of a class is the detailed explanation of how the class does its work. Clients do not know these details.
  • 4.
    3 Encapsulation A good contractpromises a well- defined area of responsibility. Each class should fully encapsulate (contain) all its responsibilities. What the class does should be clear but the details of how it does it should be hidden.
  • 5.
    3 What is datahiding? Data hiding is a term used to indicate that a class’s internal state is hidden from its clients.
  • 6.
    3 Delegation A class shouldfulfill and restrict itself to one area of responsibility. Classes should delegate outside responsibilities to other classes.
  • 7.
    3 Class Fields Class fieldshold the class data. Each field has a type: Intrinsic Types The Java language defines eight intrinsic types Java Library Types User-Defined Types Classes defined by the programmer
  • 8.
    3 Public vs. PrivateFields The keywords public and private are referred to as access modifiers. Use private keyword to hide field from clients. Use public keyword to allow clients access to field.
  • 9.
    3 Methods Methods define thebehavior of the class. Methods can return a value: Return type can be an intrinsic type or an object. Use the keyword void if a value is not to be returned by the method. Methods can accept parameters: A parameter is an object you pass in to the method when you call it. Parameters follow the same naming conventions as field names.
  • 10.
    3 Accessorand MutatorMethods Accessor methodsreturn information Typically named to indicate that a value is being returned: getAge() getAccountBalance() Mutator methods modify the state of the object. Typically named to indicate that a value is being changed: setAge() addDeposit()
  • 11.
    3 Why Use Accessorsand Mutators? Follows the concept of data hiding Allows the class designer to change how a method is implemented without rewriting clients
  • 12.
    3 What is aconstructor? A constructor is a special method that creates an instance of your class.
  • 13.
    3 Constructors Constructors initialize objectswith valid values. Constructors have special rules: May accept parameters Never marked with a return type A default constructor: Takes no parameters Is provided by the compiler if you do not provide one
  • 14.
    3 Static Members Static membersbelong to the class and are shared by all instances of the class. Declared using the static keyword Can be accessed without an instance of the class
  • 15.
    3 Dot Operator The dotoperator indicates that a method or member field (right side of dot) belongs to the object or class (left side of dot). ClassName.someStaticMemberField ObjectName.someMethod()
  • 16.
    3 The this Keyword Isa self-reference to the current object Provided only in instance methods, not in static methods