Java User-Defined Classes
Java User-Defined Classes
User-Defined Classes
Classes and Objects
OO Programming in Java
Other than primitive data types (byte, short, int, long, float, double, char,
boolean), everything else in Java is of type object.
Objects we already worked with:
Object: Operation:
my blue pen write
Object: Operation:
Acme Bank ATM withdraw
INK
Object: Attribute:
my blue pen ink amount
Object: Attribute:
Acme Bank ATM cash on hand
When you create my blue pen object, you do not have to specify its
operations or attributes. You simply say what class it belongs to.
Beware—OO people almost always use the two words, classes and
objects, interchangeably;
you need to use the context to differentiate between the two
meanings.
Account
Account Pen
Load passengers
A class An object
(the concept) (the realization)
Multiple objects
from the same class
Circle aCircle;
Circle bCircle;
aCircle bCircle
null null
Points to nothing (Null Reference) Points to nothing (Null Reference)
bCircle = aCircle;
P Q P Q
ClassName objectRefVar;
Example:
Circle myCircle1, myCircle2; //reference variables
myCircle1 = new Circle(); //calls first constructor
myCircle2 = new Circle(5.0); //calls second constructor
OR
yourCircle.radius = 100;
radius: 5.0
Create a
circle
radius: 5.0
Assign object reference
to myCircle
radius: 5.0
yourCircle no value
Declare
yourCircle
radius: 5.0
yourCircle no value
: Circle
Create a new radius: 1.0
Circle object
radius: 5.0
radius: 1.0
radius: 5.0
: Circle
Change radius in radius: 100.0
yourCircle
radius = 1
Before: After:
i 1 i 2
j 2 j 2
Before: After:
• Shallow copying: two or
c1 c1
more reference variables of
the same type point to the c2 c2
same object
• Deep copying: each c1: Circle c2: Circle c1: Circle c2: Circle
reference variable refers to radius = 5 radius = 9 radius = 5 radius = 9
its own object
To copy an object (not its reference), you need to define a method makeCopy()
1501 246 – Object Oriented Design with Java 6.37
Garbage Collection
Example on previous slide, after the assignment statement
c1 = c2; //circle objects
getRadius()
Circle
The - sign indicates
private modifier - radius: double The radius of this circle (default: 1.0).
System.out.println("Number of objects
"+myCircle.getNumberOfObjects());
}
}
A class with all private data fields and without mutators (set
methods) is not necessarily immutable. For example, the following
class Student has all private data fields and no mutators, but it is
mutable (changeable).
Both these method headings have the same name and same formal parameter
list
These method headings to overload the method methodABC are incorrect
In this case, the compiler will generate a syntax error
80
1501 246 – Object Oriented Design with Java 6.80
Difference between Passing a Primitive
Value and a Reference Value
We distinguish between:
public F() {
}
The scope of a local variable starts from its declaration point and
continues to the end of the block that contains the variable.
Java Rule:
You can declare a local variable with the same name multiple times
in different non-nesting blocks in a method, but you cannot declare
a local variable twice in nested blocks.
//block
{
double x; //illegal declaration,
//x is already declared
...
}
}