Object Life Cycle: You Create An Object From A Class by Using
Object Life Cycle: You Create An Object From A Class by Using
Creating Classes
Class
nameOfClass
{
ClassBody
}
Class Declaration
Access Level
By default, a class can be used only by
other classes in the same package.
Look in Creating and Using Packages for
information about how to use modifiers to
limit access to your classes and how it
affects your access to other classes.
6
Class Body
The class body contains all of the code that
provides for the life cycle of the objects created
from it:
constructors for initializing new objects,
declarations for the member attributes that provide
the state of the class and its objects,
Member methods to implement the behavior of the
class and its objects
Object state
The idea of "state" is that an object has
characteristics that it keeps as long as it
exists. The characteristics may change in
value during the lifetime of the object.
The state of an object is held in its
instance variables (not class variables, not
in the parameters of methods, nor in the
local variables of methods.)
8
static
final
type name
final
Indicates that the value of this member cannot
change.
13
Static variables
Example
public class StaticExample
public static int x;
public int y;
e2
e1
10
e2 object
allocation
12
Class
allocation
15
Example
public class Bicycle{
private
private
private
private
int cadence;
int speed;
int id;
static int numberOfBicycles = 0;
17
static
returnType
methodName
(paramlist)
Throws
exceptions
19
Packages
A package is a collection of related
classes and interfaces providing access
protection and namespace management:
Help know where to find groups of related
classes and interfaces.
Avoid conflict with class names in other
packages, because the package creates a
new namespace.
21
Packages
How to create a package?
// only comment can be here
package world;
public class HelloWorld {
public String sayHello() {
return Hello World;
}
}
Note: packages should map to file system, for package world you should
create directory world that contains HelloWorld class definition
22
Note: You might have to set your class path so that the
compiler and the interpreter can find the source and
class files for your classes and interfaces
23
PackagesDisambiguating a Name
If a member in one package shares the same name with a member in
another package and both packages are imported, you must refer to
each member by its qualified name.
For exmaple lets define the following
package graphics;
public class Rectangle { . . . }
The java.awt package also contains a Rectangle class. If both graphics
and java.awt have been imported, the following is ambiguous:
Rectangle rect;
In such a situation, you have to be more specific and use the member's
qualified name to indicate exactly which Rectangle class you want:
graphics.Rectangle rect; or
Java.awt.Rectangle rect;
24
private
protected
public
Package(1)
World
(1) you dont explicitly set a members access level to one of the other levels25
26
Access Control--Protected
Protected
allows the class itself, subclasses and all classes in
the same package to access the members.
Use the protected access level when it's appropriate
for a class's subclasses to have access to the
member, but not unrelated classes.
Protected members are like family secrets--you don't
mind if the whole family knows, and even a few
trusted friends but you wouldn't want any outsiders to
know.
28
Access Control--Protected
package Greek;
public class Alpha {
protected int iamprotected;
protected void protectedMethod() {
System.out.println("protectedMethod");
}
}
class Gamma {
void accessMethod() {
Alpha a = new Alpha();
a.iamprotected = 10;
// legal
a.protectedMethod();
// legal
}
}
29
Access Control--Protected
package greek;
public class Alpha {
protected int iamprotected;
protected void protectedMethod() {
System.out.println("protectedMethod");
}
}
package roman;
import greek.Alpha; //needed only to find
//the symbol Alpha
class Gamma extends Alpha {
void accessMethod() {
Alpha a = new Alpha();
a.iamprotected = 10;
// legal
a.protectedMethod();
// legal
}
}
30
// legal
// legal
32
34
35
36
http://java.sun.com/docs/books/tutorial/java/jav
aOO/classes.html
http://java.sun.com/docs/books/tutorial/java/jav
aOO/classvars.html
37