Title: More on Objects
1More on Objects
More, more, more on Objects
2Where Do You Want To Go Today?
- Building new objects
- Accessors
- this and That
3Caution, Objects Under Construction!
- Objects have structure and structure has to be
built
- new means create a new instance of an object
- How are objects actually built?
- Objects know how to do things, including how to
build themselves
4Constructor Methods
- Constructors are special methods that build new
objects
- Declaration looks just like a method declaration
that has no result type
- public class Circle
- double x_center, y_center, radius
- public Circle(double x, double y, double r)
- x_center x
- y_center y
- radius r
-
5A Few Notes on Constructors I
- Don't have to declare a constructor
- Java creates a default constructor
- public class Circle
- public double x_center, y_center, radius
- // Uses the default constructor
- public double Area()
- return 3.141592654 radius radius
-
6A Few Notes on Constructors II
- Constructors have the same name as the class
- No return type (why not?)
- Constructor declarations are not members
- Never inherited, so no hiding or overriding (More
on this later)
7Multiple Constructors
- public Circle(double x, double y, double r)
- x_center x
- y_center y
- radius r
-
- public Circle()
- x_center 0
- y_center 0
- radius 1
-
- public Circle(double x, double y)
- x_center x
- y_center y
- radius 1
8Handling Multiple Constructors
- How does Java tell the constructors apart?
- Method signatures
- Signatures include
- Name of the method
- Number formal parameters
- Types of formal parameters
- Doesn't include the name of the parameter
9Method Signatures
- For this method
- public Circle(double x, double y, double r)
- Signature is
- Circle(double, double, double)
- Different methods, same signature
- public Circle(double x, double y)
- public Circle(double x, double r)
10Back to Multiple Constructors
- Multiple constructors are okay
- Each constructor has to have a unique signature
- (All of a class's methods have to have unique
signatures, not just constructors)
- Method overloading multiple versions of the same
method (but with different signatures)
11The Linda Tripp Problem
- How do we keep private data private?
- Avoid having object1 mess with object2's data
- Example Student objects know student's grades,
but one student shouldn't modify another
student's grades.
- Storing invalid data
- Stuffing 12-hr time into a 24-hr time object
12If It's Public...
- Start with
- public class Circle
- public double x_center, y_center
- public double radius, area
- public Circle(double x, double y)
- x_center x
- y_center y
- radius 1
- area 3.141592654
-
13other objects can mess with it
- Create a circle
- Circle duPont new Circle(100, 100)
- // Change the area, but leave r the same
- duPont.Area 11
- // Now a circle with radius 1 has area 11
14Using private
- Declare variables to be private
- public class Circle
- private double x_center, y_center
- private double radius, area
-
-
-
- Now only Circle can modify the variables
(x_center, y_center, ...)
15Granting Access
- Other classes might need to read or write the
values stored in private members
- Create accessor functions
- Set (or settor)
- Get (or gettor)
- Control access through get and set
16Setting and Getting
- What's the difference between using get and set
methods, and just making everything public?
- Two benefits
- Protect instance variables from outside meddling
- Insulates class users from changes in variables
17Example from Fig 6.5
// blah, blah, blah. Set invalid values to zero.
public void setTime( int h, int m, int s )
setHour( h ) // set the hour
setMinute( m ) // set the minute
setSecond( s ) // set the second
// set the hour public void setHou
r( int h ) hour ( ( h 0 h 0 ) // set the minute public void
setMinute( int m ) minute ( ( m 0 m 60 ) ? m 0 ) // set the second
public void setSecond( int s )
second ( ( s 0 s
18Me, a Name I Call Myself
- How can objects refer to themselves?
- Every object has a built-in reference to itself,
called this
- this can be used only in
- Body of a method or constructor
- Initializer of an instance variable
- Anywhere else is a compile-time error
19This is Bad,
- public class Circle
- public double x, y
- public double r, area
- public Circle(double x, double y)
- x x // This doesn't do much, because we're
- y y // just assigning parameters to
themselves
- r 1
- area 3.141592654
-
20But this is okay
- public class Circle
- public double x, y
- public double r, area
- public Circle(double x, double y)
- this.x x // Now we're assigning the instance
- this.y y // variables to the parameters
- r 1
- area 3.141592654
-
Better to avoid this mess altogether by using
different names for data members and method
parameters