Upload
osborne-norton
View
219
Download
0
Tags:
Embed Size (px)
Citation preview
Starting
Chapter 4
Starting
1 Course Outline* Covered in first half until Dr. Li takes over.
JAVA and OO:
Review what is Object Oriented ProgrammingHow it is implemented in Java.
Why we use Javasome strengths of the language.some history.
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.
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
Java core librariesHow to structure code solutions in JavaPatterns and anti-PatternsJava collections and generics
But not in detail
Assume familiar with an OO language.
Except for ….
Not so trivial and worth giving you an introduction
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 objectcar, person, book
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
It will have methods : these change state and/or perform operations and calculations.
Methods are accessed by sending messages to the objects.
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.
Object
State
Methods
Messages
Class : Instantiation
Starting
4 Classes Class:
A class is a recipe or blueprint for an objectA 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 aboutWell designed classes are
good for implementationgood 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
Eases use – no need to understand implementationEases development – implementation can be altered as often as required with no impact on the user.
Implementation is flexible, interface should be fixed
Class
Persistance
Useful throughout the process
Encapsulation core part of OO
Other meanings
Persistance
Design to the Interfacce
3 Principles Starting
5 OO description
3 Principles Starting
6 …
3 Principles Starting
7 Object creation*
Objects are created. Once created they have an independent existance.
And the objects themselves may create further objects or groups of objects
Instantiated from classes
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 : coherenceOne object should be (as far as possible) completely independent of all others.
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.
Inheritance allows us to use an old class simplywithout disturbing the operation of the
base classbenefitting from improvements in the base class
Need to make clear these concepts and we will start by using BlueJ
Object isolation
Encapsulation
Inheritance
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 lineSDK – eclipse, netbeans, ….. BlueJ
BlueJ a beginners development environment.Graphical representation of the programme.Environment is free to download
Quite sophisticated development is possibleSimple debugger
Standalone running of the programmes which have been developedEasy 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 documentationTo create an object a constructor method is used.Provided by the JVM if not present
public class car() {}
Constructorpublic car(){}
Add some state
Always put a default constructor in explicitly.
Minimum class
JAVADOC – tool shipped as part of the SDK
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.
One primitive data type is double
Let us add a method.
double
Starting
13 Outputting* output
Can be done usingSystem.out.println(): puts out 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.
System.out.println() of an object actually prints out the string from toString().
For useful output we need to override the toString method.
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
JVM takes care of turning doubles to strings
Here it is the same type
Overriding
Starting