CS535 Object-Oriented Programming & Design
Fall Semester, 1996
Java Classes and Objects
[To Lecture Notes Index]
San Diego State University -- This page last updated Tuesday, 10 September, 1996
Contents of Java Classes and Objects
- References
- Classes
- Basic Terms
- Fields
- Constructors - Initializing Fields
- this
- Access Levels for Fields and Methods
- Object Variables are References
- Parameter Passing - By value only
- Static Fields and Methods
- Constants
- Class Names, Packages and Import
Core Java, Chapter 4
The Java Programming Language, Arnold, Gosling, 1996 Chapter 2, 10
Sun Java Compiler V 1.0.2
MetroWorks Java Compiler R9
Quote
First things first, but not necessarily in that order.
- Dr. Who, Meglos
Language Level Definition
Conceptual Level Definition
Java Classes and Objects Slide # 3
Java | field | method |
C++ | data member | member function |
Smalltalk | instance variable | method |
C | ??? | function |
Class member refers to either a field or method
class BankAccount
{
public float balance = 0.0F;
public void deposit( float amount )
{
balance += amount ;
}
}
Comparisons to C++
Similar class syntax and structure
No multiple inheritance, uses interfaces instead
Functions are virtual by default
Constructors are much simpler
Destructors are not need
Packages provide separate name spaces for classes
Java Classes and Objects Slide # 4
class BankAccount
{
public float balance = 0.0F;
}
class RunBank
{
public static void main( String args[] )
{
System.out.println( " Start main " );
BankAccount richStudent = new BankAccount( );
richStudent.balance = (float) 100000;
BankAccount poorInstructor = new BankAccount( );
poorInstructor.balance = 5.10F;
System.out.println( "Student Balance: " + richStudent.balance );
System.out.println( "Prof Balance: " + poorInstructor.balance );
}
}
Output
Start main
Student Balance: 100000
Prof Balance: 5.1
Java Classes and Objects Slide # 5
Running Above Program
Put classes BankAccount and RunBank in file BankExample.java
rohan 50-> ls
BankExample.java
rohan 51-> javac BankExample.java
rohan 52-> ls
BankAccount.class BankExample.java RunBank.class
rohan 53-> java RunBank
Start main
Student Balance: 100000
Prof Balance: 5.1
Note
- Each class compiles to its own .class file
- Execute the class with the main program
Java Classes and Objects Slide # 6
Multi-File Programs
Put class BankAccount in file BankAccount.java
Put class RunBank in file RunBank.java
Compile top level class - javac will compile needed classes
Example
rohan 13-> ls
BankAccount.java RunBank.java
rohan 14-> java -cs RunBank
Start main
Student Balance: 100000
Prof Balance: 5.1
rohan 15-> ls
BankAccount.class RunBank.class
BankAccount.java RunBank.java
Java Classes and Objects Slide # 7
Methods
class BankAccount
{
public float balance = 0.0F;
public void deposit( float amount )
{
balance += amount ;
}
public String toString()
{
return "Account Balance: " + balance;
}
}
class RunBank
{
public static void main( String args[] )
{
BankAccount richStudent = new BankAccount( );
BankAccount poorInstructor;
poorInstructor = new BankAccount( );
richStudent.deposit( 10000F);
poorInstructor.deposit( 5.10F );
System.out.println( "Student Balance: " + richStudent.balance );
System.out.println( "Prof: " + poorInstructor.toString() );
}
}
Output
Student Balance: 10000
Prof: Account Balance: 5.1
Java Classes and Objects Slide # 8
The toString() Standard
- When required Java sends toString() message to objects to convert them to
strings
- This happens in println() and when adding to strings
class RunBank
{
public static void main( String args[] )
{
BankAccount richStudent = new BankAccount( );
BankAccount poorInstructor = new BankAccount( );
richStudent.deposit( 10000F);
poorInstructor.deposit( 5.10F );
String profBalance = "Prof: " + poorInstructor;
System.out.println( profBalance );
System.out.println( "Prof: " + poorInstructor );
System.out.println( richStudent );
}
}
Output
Prof: Account Balance: 5.1
Prof: Account Balance: 5.1
Account Balance: 10000
Java Classes and Objects Slide # 9
class BankAccount
{
public float balance;
public BankAccount( float initialBalance ) //Constructor
{
balance = initialBalance;
}
public void deposit( float amount )
{
balance += amount ;
}
public String toString()
{
return "Account Balance: " + balance;
}
}
class RunBank
{
public static void main( String args[] )
{
BankAccount richStudent = new BankAccount( 10000F );
BankAccount poorInstructor = new BankAccount( 5.10F );
System.out.println( "Prof: " + poorInstructor );
System.out.println( "Student: " + richStudent );
}
}
Output
Prof: Account Balance: 5.1
Student: Account Balance: 10000
Java Classes and Objects Slide # 10
Multiple Constructors
class ConstructorExample {
public ConstructorExample( ) {
System.out.println( "In constructor - no argument" );
};
public ConstructorExample( int size) {
System.out.println( "In constructor - one argument" );
};
public void ConstructorExample( ) {
System.out.println( "return type means it is ");
System.out.println( "not a constructor " );
};
}
class TestConstructor {
public static void main( String args[] ) {
System.out.println( " Start main " );
ConstructorExample test = new ConstructorExample( );
ConstructorExample x = new ConstructorExample(5);
System.out.println( " Done with Constructors " );
test.ConstructorExample ();
}
}
Output
Start main
In constructor - no argument
In constructor - one argument
Done with Constructors
return type means it is
not a constructor
Java Classes and Objects Slide # 11
Implicit Constructors
If a class has no constructor compiler generates an implicit constructor with
no arguments
class ImplicitConstructorOnly {
int size = 5;
}
class OneConstructor {
OneConstructor( String message ) {
System.out.println( message );
}
}
class TwoConstructors {
TwoConstructors ( String message ) {
System.out.println( message );
}
TwoConstructors ( ) {
System.out.println( "No argument Constructor" );
}
}
class Constructors {
public static void main( String args[] ) {
ImplicitConstructorOnly ok = new ImplicitConstructorOnly();
TwoConstructors alsoOk = new TwoConstructors();
OneConstructor compileError = new OneConstructor();
}
}
Java Classes and Objects Slide # 12
Overloading Methods
The signature of a methods is its name with number, type and order of its
parameters.
The return type is not part of the signature of a method.
Two methods in the same class can have the same name if their signatures are
different.
class OverLoad
{
public void same() {
System.out.println( "No arguments" );
}
public void same( int firstArgument ) {
System.out.println( "One int arguments" );
}
public void same( char firstArgument ) {
System.out.println( "One char arguments" );
}
public int same( int firstArgument ) { // Compile Error
System.out.println( "One char arguments" );
return 5;
}
public void same( char firstArgument, int secondArgument) {
System.out.println( "char + int arguments" );
}
public void same( int firstArgument, char secondArgument ) {
System.out.println( "int + char arguments" );
}
}
Java Classes and Objects Slide # 13
(not that)
this
- Refers to the object on which the method operates
-
- Used to return self from method and pass self as a parameter
-
- Java's this differs from C++'s this
-
- The difference occurs with inheritance
class BankAccount
{
public float balance;
public BankAccount( float initialBalance )
{
this.balance = initialBalance;
}
public void deposit( float amount )
{
balance += amount ;
}
public String toString()
{
return "Account Balance: " + balance;
}
}
Java Classes and Objects Slide # 14
Returning this
class BankAccount
{
public float balance;
public BankAccount( float initialBalance )
{
this.balance = initialBalance;
}
public BankAccount deposit( float amount )
{
balance += amount ;
return this;
}
}
class RunBank
{
public static void main( String args[] )
{
BankAccount richStudent = new BankAccount( 10000F );
richStudent.deposit( 100F ).deposit( 200F ).deposit( 300F );
System.out.println( "Student: " + richStudent.balance );
}
}
Output
Student: 10600
Java Classes and Objects Slide # 15
this as Parameter
A Convoluted Contrived Example
class CustomerList
{
public BankAccount[] list = new BankAccount[ 100 ];
public int nextFreeSlot = 0;
public void add( BankAccount newItem )
{
list[ nextFreeSlot++ ] = newItem;
}
}
class BankAccount
{
public float balance;
public BankAccount( float initialBalance )
{
this.balance = initialBalance;
}
public void badBalanceCheck( CustomerList badAccounts )
{
if ( balance <= 0F ) badAccounts.add( this );
}
}
class RunBank
{
public static void main( String args[] )
{
BankAccount richStudent = new BankAccount( 10000F );
CustomerList customersToDrop = new CustomerList();
richStudent.badBalanceCheck( customersToDrop );
}
}
Java Classes and Objects Slide # 16
Finalize - Destructor of Sorts
Automatic storage management handles reclaiming objects and arrays that are no
longer needed by a running program
When an object is determined to no longer be needed it may be reclaimed, unless
it has a finalize method
If a class has a finalize method, the method is executed. The object is
reclaimed the next time it is determined the object is no longer needed
The finalize method is never called more than once for an object
Java Classes and Objects Slide # 17
Finalize Example
class Death {
int myId;
public Death ( int sequenceNumber) {
myId = sequenceNumber; }
public void finalize( ) {
System.out.println( myId ); }
}
class Finalization {
public static void main( String args[] ) {
Death sampleObject;
for ( int k = 0; k < 5; k++ )
sampleObject = new Death( k );
}
}
No Output
class FinalizationForced {
public static void main( String args[] ) {
Death sampleObject;
for ( int k = 0; k < 5; k++ )
sampleObject = new Death( k );
System.gc();
System.runFinalization(); }
}
Output
0
1
2
3
4
Java Classes and Objects Slide # 18
public
- Members declared public are accessible anywhere the class is accessible
-
- Inherited by all subclasses
protected
- Members declared private are accessible to an inherited by subclasses, and
accessible by code in the same package
private
- Members declared private are accessible only in the class itself
If no access level is given
-
- Members declared with on access modifier are accessible only to code in the
same package
Java Classes and Objects Slide # 19
Public, Protected, Private
class AccessLevels
{
public int publicObjectVariable ;
protected float protectedObjectVariable = 10;
private int[] privateObjectVariable;
int noExplicitAccessLevel = publicObjectVariable;
public AccessLevels ( int startValue )
{
System.out.println( " Start Constructor" );
privateObjectVariable = new int[ startValue ];
}
public void sampleMethod( int value )
{
System.out.println( " In method" );
privateObjectVariable[ 1 ] = value;
}
}
class TestAccessLevels
{
public static void main( String args[] )
{
AccessLevels test = new AccessLevels ( 11 );
test.publicObjectVariable = 100; // Ok
test.protectedObjectVariable= 100; // Ok
test.privateObjectVariable[ 1 ] = 100; // Compile Error
test.noExplicitAccessLevel = 100; // Ok
}
}
Java Classes and Objects Slide # 20
Look Ma, Hidden Methods
class Hide
{
public void publicAccess()
{
System.out.println( "Start public access " );
internalWorker();
realPrivateWorker();
}
protected void internalWorker()
{
System.out.println( "In internal worker " );
}
private void realPrivateWorker()
{
System.out.println( "In Private worker " );
}
public static void main( String[] args )
{
Hide me = new Hide();
me.publicAccess();
}
}
Output
Start public access
In internal worker
In Private worker
Java Classes and Objects Slide # 21
Better BankAccount
class BankAccount
{
private float balance;
public BankAccount( float initialBalance )
{
balance = initialBalance;
}
public void deposit( float amount )
{
balance += amount ;
}
public String toString()
{
return "Account Balance: " + balance;
}
}
Java Classes and Objects Slide # 22
Modular Design Rules
Measure Twice, Cut Once
Object-Oriented programming requires more early planning then procedural
programming
Supports:
Decomposability Composability Protection Continuity Understandability
Poker Rule: Hide Your Cards
or
Information Hiding
All information about a module should be private unless it is specifically
declared public
Supports:
Decomposability Composability Continuity Understandability
Java Classes and Objects Slide # 23
Two Views on Hidden fields
- All fields should be protected or private to outside access
- All fields should be hidden from methods in same class
class BankAccount
{
private float balance;
protected float getBalance()
{
return balance;
}
protected void setbalance( float newBalance)
{
balance = newBalance;
}
public BankAccount( float initialBalance )
{
setbalance( initialBalance );
}
public void deposit( float amount )
{
setbalance( getBalance() + amount );
}
public String toString()
{
return "Account Balance: " + getBalance();
}
}
Java Classes and Objects Slide # 24
class Student
{
public char grade;
}
class PointerTest
{
public static void main( String args[] )
{
Student sam = new Student();
sam.grade = 'A';
Student samTwin = sam;
samTwin.grade = 'C';
System.out.println( sam.grade );
}
}
Output
C
Java Classes and Objects Slide # 25
class Parameter
{
public void noChangeArgument( char grade )
{
grade = 'A';
};
};
class TestParameter
{
public static void main( String args[] )
{
char samGrade = 'C';
Parameter Test = new Parameter();
Test.noChangeArgument( samGrade );
System.out.println( samGrade );
};
};
Output
C
Java Classes and Objects Slide # 26
How to get Functions to return values
- Use return
- Use references (arrays or objects)
class TestParameter
{
public static void main( String args[] )
{
char samGrade = 'C';
samGrade = inflateGrade( samGrade );
System.out.println( samGrade );
};
public static char inflateGrade( char grade )
{
switch ( grade )
{
case 'A':
case 'B':
return 'A';
case 'C':
return 'B';
case 'D':
case 'F':
return 'C';
};
return 'A';
};
};
Output
B
Java Classes and Objects Slide # 27
How to get Functions to return values
Using References - Array
class TestParameter
{
public static void main( String args[] )
{
char[] classGrades = new char[ 30 ];
classGrades[ 1 ] = 'B';
TestParameter.changeArgument( classGrades );
System.out.println( classGrades[ 1 ] );
};
static public void changeArgument( char[] grade )
{
grade[1] = 'A';
};
};
Output
A
Java Classes and Objects Slide # 28
How to get Functions to return values
Using References - Class
class Student
{
public char grade;
};
class Parameter
{
public void changeArgument( Student lucky )
{
lucky.grade = 'A';
};
};
class TestParameter
{
public static void main( String args[] )
{
Student sam = new Student();
sam.grade = 'B';
Parameter Test = new Parameter();
Test.changeArgument( sam );
System.out.println( sam.grade );
};
};
Output
A
Java Classes and Objects Slide # 29
Static fields are class variables
- The same static field is shared between all object of the class
-
- Static fields exist before creating an object of the class
-
- Same as C++ static data members
class StaticFields
{
public static int size = 10;
public void increaseSize( int increment )
{
size = size + increment;
}
}
class DemoStaticFields
{
public static void main( String[] arguments )
{
System.out.println( "Size " + StaticFields.size );
StaticFields top = new StaticFields();
StaticFields bottom = new StaticFields();
top.size = 20;
System.out.println( "Size " + bottom.size );
}
}
Output
Size 10
Size 20
Java Classes and Objects Slide # 30
Static Methods
class StaticMethods
{
public static int size = 10;
public static void increaseSize( int increment )
{
size = size + increment;
}
}
class DemoStaticMethods
{
public static void main( String[] arguments )
{
StaticMethods.increaseSize( 30 );
System.out.println( "Size " + StaticMethods.size );
StaticMethods top = new StaticMethods();
top.increaseSize( 20 );
System.out.println( "Size " + StaticMethods.size );
}
}
Output
Size 40
Size 60
Java Classes and Objects Slide # 31
More Static
class StaticFields
{
public static int size = 10;
protected static int[] classArray = new int [ size ];
public int localInt;
static // Run when class is first loaded
{
size = 20;
};
public static void classMethod( int value )
{
System.out.println( " In class method" );
size = value;
localInt = value; // Compile Error
}
}
class TestStaticFields
{
public static void main( String args[] )
{
StaticFields test = new StaticFields ( );
StaticFields secondObject = new StaticFields ( );
StaticFields.size = 100;
System.out.println( test.size ); // Print 100
StaticFields.localInt = 99; // Compile Error
test.localInt = 99;
}
}
Java Classes and Objects Slide # 32
Main revisited
class Top
{
public static void main( String[] arguments )
{
System.out.println( "In top main" );
Bottom.main( arguments );
}
public static void print()
{
System.out.println( "In top" );
}
}
class Bottom
{
public static void main( String[] arguments )
{
System.out.println( "In bottom main" );
Top.print( );
OddMain( arguments );
}
}
class OddMain
{
public static int main( String[] arguments )
{
System.out.println( "In odd main" );
Top hat = new Top();
String[] message = { "Hi", "Mom" };
hat.main( message );
return 5;
}
}
Java Classes and Objects Slide # 33
final variables are constants
class Constants
{
public static final int SIZE = 10;
protected static final int[] CLASS_ARRAY = new int [ size ];
public final int MUST_INITIALIZE_HERE; // Compile Error
public final char grade = 'A';
public void trouble()
{
final int willNotWork = 123; // Compile Error
}
}
class TestConstants
{
public static void main( String args[] )
{
System.out.println( Constants.SIZE ); // Prints 10
Constants test = new Constants();
System.out.println( test.grade ); // Prints A
System.out.println( Constants.grade ); // Compile Error
}
}
Java Classes and Objects Slide # 34
Each class belongs to a "package"
A package creates a name space
A package defines the full name of a class
Standard packages
java.applet | java.awt.peer | java.net |
java.awt | java.io | java.util |
java.awt.image | java.lang | sun.tools.debug |
Example - PrintStream
PrintStream is in the java.io package
The full name of the class is java.io.PrintStream
class Output {
public static void main( String args[] ) {
java.io.PrintStream myOut = System.out;
myOut .print( "Look Mom, No System.out" );
}
}
Java Classes and Objects Slide # 35
Import Statement
The import statement allows you to shorten class names
import java.io.PrintStream;
class Output {
public static void main( String args[] ) {
PrintStream myOut = System.out;
myOut.print( "Look Mom, No System" );
}
}
import java.io.*;
class Output {
public static void main( String args[] ) {
PrintStream myOut = System.out;
myOut.print( "Look Mom, No System" );
}
}
Default Import
All classes in the java.lang are imported in all programs by default
Java Classes and Objects Slide # 36
Placing a class in a package
package sdsu.roger;
public class Sample {
public void hello() {
System.out.println( "Hello for package sdsu.roger" );
}
}
Place
program in file named "Sample.java"
Place file "Sample.java" in directory called "roger"
Place directory "roger" in directory called "sdsu"
Place directory "sdsu" in "~whitney/java/classes"
Make sure that "~whitney/java/classes" in the CLASSPATH environment variable
- setenv CLASSPATH
-
- '.:/opt/java/classes:/home/ma/whitney/java/classes'
Place the following class anywhere you like and compile
import sdsu.roger.Sample;
class TestPackage {
public static void main( String args[] ) {
Sample me = new Sample();
me.hello();
}
}
Java Classes and Objects Slide # 37
Name Collision
File SearchTree/Leaf
package SearchTree;
public class Leaf
{
public Leaf()
{
System.out.println( "Leaf in a binary search tree" );
}
}
File Botany/Leaf
package Botany;
public class Leaf
{
public Leaf()
{
System.out.println( "Leaf in a real tree" );
}
}
First Main
class Test
{
public static void main( String args[] )
{
Botany.Leaf green = new Botany.Leaf();
SearchTree.Leaf node = new SearchTree.Leaf();
}
}
Java Classes and Objects Slide # 38
Second Main
import SearchTree.Leaf;
class Test
{
public static void main( String args[] )
{
Botany.Leaf green = new Botany.Leaf();
Leaf node = new Leaf();
}
}
Third Main
import SearchTree.Leaf;
import Botany.Leaf; // Compile error
class Test
{
public static void main( String args[] )
{
Botany.Leaf green = new Botany.Leaf();
Leaf node = new Leaf();
}
}
What Should this do Main
import SearchTree.Leaf;
import Botany.*;
class Test
{
public static void main( String args[] )
{
Botany.Leaf green = new Botany.Leaf();
Leaf node = new Leaf();
}
}
Java Classes and Objects Slide # 39
Class Access Levels
Public
- Accessible to code in and outside a package
Package
- Accessible to code in package only
- No subclasses outside package
package Botany;
public class Leaf
{
public Leaf()
{
System.out.println( "Leaf in a real tree" );
}
}
package Botany;
class BotanyHelper
{
// Only code in package Botany can use this class
}
Java Classes and Objects Slide # 40
Package Notes
If a class has no declared package, it is in the unnamed package
CLASSPATH needs to point to the root directory containing the binaries of
packages
There is no requirement to have both binaries and source code of a package in
the same location.