95712C Lecture Notes Wednesday, September 15, 2004: Objects and Classes
95712C Lecture Notes Wednesday, September 15, 2004: Objects and Classes
Characteristics of OOP
OOP enables you to represent elements of the problem domain in the language, referred
to as objects. Characteristic of object oriented programs (see Thinking in Java)
1. Everything is an object. You can take any conceptual component in the problem
youre trying to solve (dogs, buildings, services, etc.) and represent it as an object
in your program.
2. A program is a bunch of objects telling each other what to do by sending
messages.
3. Each object has its own memory made up of other objects. You create a new kind
of object by making a package containing existing objects.
4. Every object has a type. Each object is an instance of a class, in which class is
synonymous with type. The most important distinguishing characteristic of a
class is What messages can you send to it?
5. All objects of a particular type can receive the same messages. This is actually a
loaded statement because of many interfaces an object may implement.
Class
Object
Abstraction
Encapsulation
Constructor
Instance member/field
Method
Setter/Getter method
Class
Since a class describes a set of objects that have identical characteristics (data elements)
and behaviors (functionality), a class is really a data type. You extend the programming
language by adding new data types specific to your needs. The programming system
welcomes the new classes and gives them all the care and type-checking that it gives to
built-in types.
Object
Once a class is established, you can make as many objects of that class as you like, and
then manipulate those objects as if they are the elements that exist in the problem you are
Object
Type
variable
One
Many
Abstract
Real
Abstraction
Abstraction is the representation of an element of the problem domain in the
programming language. A simple example might be a representation of a light bulb:
Encapsulation
Encapsulation is hiding the data, that is the instance variables, from the outside of the
class. This can be done by
1. Having private members, and
2. Providing public methods that access and modify the private members in a
controlled way.
Constructors
Constructors are used for providing the initial state of the class object. They can only be
used with the new operator.
Instance Members/Fields
These are non-static variables. There are duplicated for each object. Therefore, each
object has its own instance variables.
Methods
These are the non-static methods that are to be invoked with an object. Implicitly, the
object that invokes the method is passed as a parameter to the method. Indeed, it is the
first parameter in the run-time system and can be accessed with the this keyword.
Remember that a static method cannot refer to instance variables of its class and cannot
have this reference. The static variables and the static methods are independent of any
object of a class. However, an instance method can refer to the class variables.
Methods can be overloaded to have different type and number of arguments. They cannot
be overloaded just based on the return type.
See StaticTest.java
Order
-uses
*
Account
1
RushOrder
We will study the has-a and the uses-a relation today. In the has-a relation, objects of
class A contains objects of class B. In many situations, objects of class B come into
existence with the creation of objects of A and they disappear when objects of class A
disappear. Sometimes, objects of class A manages the lifetime of objects of class B, that
is, objects of class A creates objects of class B, use them and dispose them. Note that,
however, in Java, object disposal is not an explicit operation.
In the uses-a relation, an object of class A has a reference to an object of class B. Object
of class A doesnt create object of class B. Rather, object of class B may exist without an
object of class A. Most of the time, when an object of class A is being constructed, the
reference to object of class B is passed to the constructor of object A. Also, it is common
that a reference to object B is attached to the object A via a setter method.
Your new class can be made up of any number and type of other objects, in any
combination that you need to achieve the functionality desired in your new class. Because
you are composing a new class from existing classes, this concept is called composition
(if the composition happens dynamically, its usually called aggregation). Composition is
often referred to as a has-a relationship, as in a car has an engine.
Composition comes with a great deal of flexibility. The member objects of your new class
are typically private, making them inaccessible to the client programmers who are using
the class. This allows you to change those members without disturbing existing client
code. You can also change the member objects at run time, to dynamically change the
behavior of your program. Inheritance does not have this flexibility since the compiler
must place compile-time restrictions on classes created with inheritance, as we shall see
next week.
Parameter Passing
In Java, the convention of parameter passing is always call-by-value. This is true for
primitive types as well as for object references. However, note that, when an object
reference is passed to a method as a parameter, the object pointed to by the reference may
be modified, although the reference itself cannot be modified.
See the ParamTest.java.
Object Initialization
Reference: Chapter 4 of Core Java, Chapters 1 & 2 of Thinking in Java.
See the Lecture Slides.
See ConstructorTest.java
Object Destruction
Java doesnt allow you to explicitly delete an object. This limits the programmers who
want to automate resource acquisition and release through the object constructors and
destructors.
The garbage collector, from time to time, deletes the no longer referenced objects. You
can add a finalize method into your class. It will be invoked before the garbage collector
deletes the object.
StringBuffer Class
When a String object is created, its content remains the same. The StringBuffer class can
create dynamic strings i.e. modifiable, extendable in runtime. In other words, String
objects are constant strings and StringBuffer objects are dynamic strings.
buf = new StringBuffer() creates an empty StringBuffer
buf = new StringBuffer(10) creates a StringBuffer of size 10 with no characters in it
buf = new StringBuffer("Hello") creates a StringBuffer containing the string Hello.
toString() method will convert StringBuffer objects into String objects so that they can be
printed with PrintStream methods.
length() returns the number of characters of the StringBuffer.
setLength() increases or decreases the length of the StringBuffer.
The StringBuffer has a number of overloaded append methods to allow primitive data
types and the Strings to append to the end of StringBuffer. The append method will
return the StringBuffer objects
buf.append("hello");
buf.append(` `);
buf.append(true);
buf.append(10).append(2.5); // chaining.
The insert(index, m) methods allow to insert primitive data types and strings at any
position of StringBuffer. The index should be >= 0 and <= length of String buffer, or
otherwise a StringIndexOutOfBoundsException is generated. Like append, insert returns
the StringBuffer object.
See http://java.sun.com/j2se/1.4.2/docs/api/index.html for StringBuffer class.
Comments
Reference: Chapter 4 of textbook
There are documentation comments and implementation comments in Java. You can
generate HTML documentation out of your documentation comments in your source files
using the javadoc utility. Comments enclosed with /** . */ are documentation
comments and are parsed by the javadoc utility in order to form the HTML
documentation. Such comments may contain free form text and some tags. A tag starts
with an @, such as @author or @param. The first sentence of the free form text should
be the summary statement. Here are some conventions:
For each class:
o describe the abstraction it represents
o have the @author tag
For each public method and select private methods:
o Describe what it does in the first sentence
o Have @param tag for each of its parameters, if any
o Have @return tag for its return value, if any
o Additionally you may include the following depending on the method [this
is a subset of operation contracts in UML documents]:
Describe the pre-conditions: Describe what has to be done before
invoking this method. You may also list the assumptions made by
this method.
Coding Convention
Reference: http://java.sun.com/docs/codeconv/
Coding convention is usually personal and to some extent company thing. Read the Sun
Systems Java coding convention. Follow it except for the class organization. Follow Core
Javas convention for class organization in the following order: Constructors, methods,
fields.
Eclipse features
Javadoc comments
Export to zip files