General form of a class slides

advertisement
Objects vs. Classes
Objects vs. Classes
class
Object A
Object B
Objects are defined in terms of classes.
Objects are instances of classes.
Object C
General Form of a class
Class classname
{
type instance-variable;
//0 or more
// . . .
type static class_variable;
{
}
Static {
// 0 or more
// instance initializer
}
// static initializer
// Constructor
classname()
{
// . . .
}
// Constructor - Multiple constructors are possible
classname( parameter-list)
{
// . . .
}
type instanceMethod(parameter-list) // 0 or more
{
// body of method
}
type static classMethod(parameter-list) // 0 or more
{
// body of method
//Inner class
}
}
Structure and elements of a class
/* A program that uses the Box class.
Call this file BoxDemo.java
Note: Two classes in one file.
*/
class Box {
double width;
double height;
double depth;
}
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
// assign values to mybox's instance variables
mybox.width = 10;
mybox.height = 20;
mybox.depth = 15;
// compute volume of box
vol = mybox.width * mybox.height * mybox.depth;
System.out.println("Volume is " + vol);
}
}
Box – 2 classes
in one file
Box1- 2 classes
in 2 files
Box2- creating 2
objects
Box3 – method
added
Box4 – method
returning value
Box 5 - method
parameterized
Box6 Constructor
Box7 – Constructor
parameterized
Box8 – resolve
name conflict
Box9 –using
this
Various ways
of constructing
a class
A Practical Example
– Integer Stack
/**
*
* This class defines an integer stack that can hold 10
values.
* Illustrates:
* <ol>
* <li>Definition of a class</li>
* <li>Stack data structure</li>
* <li>Constructor</li>
* <li>methods demonstrating the behavior of the
class</li>
* <li>Note the methods do not have modifiers</li>
* </ol>
*
* @author (your name)
* @version (a version number or a date)
*/
class Stack {
int stck[] = new int[10];
int tos;
/**
* Constructor - Initialize top-of-stack
*/
Stack() {
tos = -1;
}
/**
* Push an item onto the stack
*/
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
else
stck[++tos] = item;
}
/**
* Pop an item from the stack
*/
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}
Class Variables and Methods
 Class variable: A class variable contains information
that is shared by all instances of the class.
 For example, suppose that all bicycles had the
same number of gears.
 Defining an instance variable to hold the number
of gears is inefficient; each instance would have
its own copy of the variable, but the value would
be the same for every instance.
 Define a class variable that contains the number
of gears. All instances share this variable.
 If one object changes the variable, it changes for
all other objects of that class
 Class method: a class method can be invoked
from the class while an instance method can be
invoked for a particular instance.
 Invoked without reference to a particular object.
Class methods affect the class as a whole, not a
particular instance of the class. Also called a
static method
 Example: See class C:\jclass\AClass.java
 Note the use of qualifier
public class AClass {
public int instanceInteger = 0;
public int instanceMethod() {
return instanceInteger;
}
public static int classInteger = 0;
public static int classMethod() {
return classInteger;
}

anInstance
anotherInstance
instanceInteger: 1
instanceInteger: 2
classInteger( )
classInteger( )
classInteger
See BJ_AClass.java
Can instance access a static variable/method?
Can a static method access an instance variable?
See StaticVsInstance
Access Modifiers
Java Modifiers
Modifier
(default)
class
X
method
X
data
X
X
X
X
private
X
X
protected
X
X
static
X
X
X
public
final
X
X
abstract
X
X
native
X
synchronized
X
Explanation
A class, method, or data is
visible in this package.
A class, method, or data field is
visible to all the programs in
any package.
A method or data field is only
visible in this class.
A method, or data field is
visible in this package and in
subclasses of this class in any
package.
Defines a class method or a
class data field.
A final class cannot be
extended. A final method
cannot be modified in a
subclass. A final data field is a
constant.
An abstract class must be
extended. An abstract method
must be implemented in a
concrete subclass.
A native method indicates that
the method is implemented
using a language other than
Java.
Only one thread at a time can
execute this method
Controlling Access to Members of a Class
 An access specifier determines whether other classes
can use a particular member variable or call a
particular method
 public
o Accessible to everyone, both designer and users
of the class
o Public instance variables frowned upon
 private
o Accessible only within class implementation,
only class designer
 protected
o Similar to private, but also visible in subclasses
(classes that are designed based on this one)
 default (when you list no keyword) is “friendly”
o Public within this package, but private outside it
Specifier of Class
a member itself
within a
class
Private
x
No specifier
x
protected
x
public
x
Package Subclasses World
(classes in of the
the same class
package)
x
x
x
x
x
x
Two Considerations:
1. Access levels of the source determines which
members of the classes your class can use
2. When you write a class, you need to decide what
access level every member variable / method in your
class should have.
Storage for objects
 A Time object has two instance variables
 Object about same size as comparable structure
 Creating a Time object allocates the space
Time t = new Time();
 Each Time object has its own distinct copies of the
instance variables
 When messaging an object or accessing fields, you are
referring to a particular object’s copy of the fields
Download