Introduction
To Java - MFC 158 G
Week
8 Lecture notes - Fall 2000
Chapter 8 - Object based Programming- MFC 158 (1 of 2)
Now that we
have discussed control structures and methods, we can discuss OOP in more
detail
-
OOP
encapsulates data (attributes) and behavior (methods) into objects
-
Information
hiding - we use well defined interfaces, yet are not concerned with details of
a method (instance variables should be declared as private whenever possible)
-
We
drive a car without knowing how all the components work (engine, transmission,
etc.)
-
The
nouns in a system-requirements document help to determine the classes during
the design phase
-
The
verbs in a system-requirements document can help to determine the methods
Implementing a
Time Abstract Data Type with a Class
-
using
encapsulation, try to keep data private and manipulate the data with public
methods
-
hides
implementation details from clients, reducing bugs (easier to modify in the
future)
-
when
we extend a class (class airline
extends Japplet), Japplet is the superclass (or base class) and airline is the subclass (or derived
class)
-
using
public data is uncommon and is a
dangerous programming practice.
-
We
can include objects as instance variables of other classes (pg 357)
(Class Employee contains an instance
variable which includes a date object-which is a class)
Constructors
-
method
with the same name as the class itself
-
not
mandatory, but can be useful in setting up the creation of an object (default
constructor)
-
a
special method that initializes instance variables of a class object
-
called
automatically when an object is instantiated (Time2 t1 = new Time2(12,25,42)
)
-
using
overloading, it is possible to have several constructors (each with different
arguments)
-
instance
variables not initialized by the programmer are initialized by the compiler:
primitive
numeric
= 0, booleans are set to false, references are set to null
Time2 t1 = new Time2(12,25,42)
-
operator
new allocates memory in which the
Time2 object will be stored
-
then
new calls the Time2 constructor to
initialize the instance variables of
the new object
Communication
to Objects (set methods and get methods)
-
get
method (accessor method - used for reading a value only)
-
set
method (mutator methods - typically change a value).
-
Avoid
the use of public instance variables!
-
Can
perform validity checking.
-
Can
assure data integrity
Final Instance
Variables (used
for constants in a program - value never changes)
Private
final int MAX = 10;
Initial
values can be set ONLY in a class constructor (or when created as above)
Package
Creation - creating your own libraries (pg 336)
-
define
a public class
-
choose
a package name and add a package statement to your source code
package
com.deitel.jhtp3.ch08
-
compile the class so it gets placed the the
appropriate directory
javac
-d c:\jdk1.2\jre\classes Time1.java
(must create the classes
directory manually first!!!)
-
Creates
dir structure and class à C:\jdk1.2\jre\classes\com\deitel\jhtp3\ch08\Time1.class
-
Import
the class into your codeà import com.deitel.jhtp3.ch08.Time1;
Using the this reference
-
used
to access the instance of data belonging to a particular object
-
the
this reference is a reference to an
object (the current object being manipulated)
-
since
local variables of the same name hide instance variables, we can refer to this for obtaining values.
class
SimpleTime { private int hour, minute, second; public SimpleTime( int hour, int minute,
int second ) { this.hour = hour; this.minute = minute; this.second = second; } |
Finalizers
-
provides
a way to give resources back to the system when no longer needed.
-
Helps
to prevent memory leaks
-
When
an object is no longer used in a program
(someObject = null), the object is marked for garbage collection. (no guarantee as to when the memory is
actually returned to the system)
-
Classes
in Java can have a finalize method
that performs other (programmer defined) necessary tasks to assure program/data
integrity.
Static class
members
-
each
object has its own copy of the instance variables in the class.
-
If
we wanted to keep a variable that has class-wide scope (all objects access the
same variable), we could create a static
member/variable private static int
count; (pg 368)
-
In
a video game, each object (martian) might need to know the total instances of
martians (and take action based on that information)
-
We
can’t use the this operator to refer
to a static variable. The this reference pertains to an instance
of an object, not classwide.