starting chapter 4 starting. 1 course outline* covered in first half until dr. li takes over. java...

14
Starting Chapter 4 Starting

Upload: osborne-norton

Post on 11-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

Starting

Chapter 4

Starting

Page 2: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 3: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 4: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 5: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 6: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

5 OO description

3 Principles Starting

Page 7: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

6 …

3 Principles Starting

Page 8: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 9: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 10: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 11: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 12: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 13: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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

Page 14: Starting Chapter 4 Starting. 1 Course Outline* Covered in first half until Dr. Li takes over. JAVA and OO: Review what is Object Oriented Programming

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