Transcript Document

Chapter 4
Starting
Starting
1 Course Outline*
Covered in first half until Dr. Li takes over.
JAVA and OO:
Review what is Object Oriented Programming
How it is implemented in Java.
Assume familiar with an OO
language.
Why we use Java
some strengths of the language.
some history.
But not in detail
How can I write and run Java Programmes.
editor, SDK, compiler, java tools
Java is a very simple language:
course requirement is some familiarity with
an OO language.
Except for ….
Will review the grammar and syntax – a handful of
slides – you should be able to get what you need from a
suitable text book
Concentrate on
Not so trivial and
worth giving you an
introduction
Java core libraries
How to structure code solutions in Java
Patterns and anti-Patterns
Java collections and generics
What they are an why you
need to be familiar with
them.
Starting
2 Course
Outcomes
You will be able to write, compile and run
a simple helloworld programme.
You will have the intellectual tool kit to
turn yourself into a first rate programmer.
If you are already a first rate programmer .
well 1st rate programmers are always looking to
improve their skills and develop their toolkits
I hope I will introduce you to some new ideas,
which you can use
Starting
3 Java & OO*
JAVA and OO:
Objects are a software method of modelling an object in
the problem domain.
The object may be concrete and relate a real object
car, person, book
Object
It may be abstract and relate to a construct in the
solution domain.
It will typically have a state : a description of the
object, which may include the properties which
distinguish the object from others of the same type
State
It will have methods : these change state and/or
perform operations and calculations.
Methods
Methods are accessed by sending messages to the
objects.
Messages
Once created objects have an existence independent of
the other objects in the programme.
Objects are created by the programmer defining a class
and instantiating objects of that class.
Class : Instantiation
Starting
4 Classes
Class:
A class is a recipe or blueprint for an object
A class may be used to create many objects
An object is a coherent bundle of methods and data.
It is the dynamic realisation of a class.
Only exists while the programme is running
An object is easy to think about and talk about
Well designed classes are
Class
Persistance
Persistance
Useful throughout the
process
good for implementation
good for design and communicating ideas
The set of methods is called the interface
A message is sent to the method to perform a sequence
of operations (normally on the data)
How these operations are achieved is of interest only to
the author.
Irrelevant to the user – called encapsulation
Other meanings
Encapsulation core part of
OO
Eases use – no need to understand implementation
Eases development – implementation can be altered as
often as required with no impact on the user.
Design to the Interfacce
Implementation is flexible, interface should be fixed
Starting
3 Principles
5 OO description
Starting
3 Principles
6…
Starting
3 Principles
7 Object creation*
Objects are created. Once created they have an
independent existance.
Instantiated from classes
And the objects themselves may create further
objects or groups of objects
Starting
8 Object
communication(i)
They send messages to each other
The messages may simply change the state of
another class, or a response may be elicited. They
may return data or output data.
Starting
9 Java & OO(i)*
JAVA and OO : Design
A java object should exhibit high coherence and weak
coupling.
All the information about that “object” should reside in
one place : coherence
One object should be (as far as possible) completely
independent of all others.
Object isolation
Independence promotes development and reuse.
Use the object in another context.
Develop the object with no reference to users.
Encapsulation (data hiding) the internals of an object
should not be visible to the outside world.
Inheritance all the methods and state of an class can
be used as the basis of a new class.
Encapsulation
Inheritance
Inheritance allows us to use an old class
simply
without disturbing the operation of the base
class
benefitting from improvements in the base
class
Need to make clear these concepts and we will start by
using BlueJ
Starting
10 BlueJ*
JAVA and OO : Programme creation and execution
A number of ways to create, develop and execute java
code.
Simple editor and the command line
SDK – eclipse, netbeans, ….. BlueJ
BlueJ a beginners development environment.
Graphical representation of the programme.
Environment is free to download
Quite sophisticated development is possible
Simple debugger
Standalone running of the programmes which have
been developed
Easy migration to netbeans.
http://www.bluej.org/
Starting
11 Creating
Objects*
Classes and Objects
A class may contain almost nothing.
Javadoc may be used to create documentation
To create an object a constructor method is used.
Provided by the JVM if not present
public class car() {
}
JAVADOC – tool shipped
as part of the SDK
Minimum class
Constructor
public car(){
}
Add some state
Always put a default constructor in explicitly.
Starting
12
Documentation*
Using JAVADOC
Comments in a java file are
//
single line
/*
everything between
*/
Comments with special format are picked up by
javadoc and put in a suitable place in the
documentation.
Javadoc comments are about the interface – the only
thing the user of a class cares about. Comments
internal to the code are ignored.
Constructors return only a pointer to the object
created.
double
One primitive data type is double
Let us add a method.
Starting
13 Outputting*
output
Can be done using
System.out.println():
puts out strings
JVM takes care of turning
doubles to strings
Lets look at creating a object inside another object
When a object is created if you want to refer to it you
need to create a pointer of a suitable type to refer to it.
Here it is the same type
System.out.println() of an object actually prints out the
string from toString().
For useful output we need to override the toString
method.
Overriding
The ability to replace the method of a superclass with a
method of the subclass is crucial to OO programming.
Relevant to sub class without messing up superclass
Starting