Topic1 ClassesAndObjects

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Objects and Classes

What are objects and classes? Creating objects multiple instances Interacting with objects methods parameters data types Source code
compilation v. interpretation

the edit-compile-run cycle


Autumn 2006 Introduction to Programming

What are objects and classes?


Object:
Represents a thing from the real world, or from some problem domain (e.g. the red car down there in the car park)

Class:
describes, abstractly, all objects of a particular kind (e.g. the class of all cars)

Autumn 2006

Introduction to Programming

Classes
A class describes a kind of object e.g. Car It specifies what sorts of information (attributes) we have about these kinds of object e.g. make, colour, speed, location It specifies the allowable behaviours and operations on these kinds of object (methods) e.g. move, stop, accelerate, wipers on/off
Autumn 2006 Introduction to Programming

Objects
What colour is the car?
which car?

Can only answer the question with respect to a specific car!


can have lots of different objects of class Car each car object will have its own colour, make, location, etc.
Autumn 2006 Introduction to Programming

Creating Objects

Shapes Example

Autumn 2006

Introduction to Programming

Fundamental Concepts
class - Circle, Triangle object - triangle1, square23 constructor - new Circle() method - makeVisible() parameter - changeSize(int newSize) field - private String color, private int size data type - String color, int size
Autumn 2006 Introduction to Programming

Methods and Parameters


objects have operations (behaviours) which can be invoked (Java calls them methods). methods may have parameters to pass additional information needed to perform some behaviour. each parameter has a specific type, and the value given for that parameter must be of the appropriate type. constructors are also methods (question: what makes them different to the other methods?) and may have parameters too

Autumn 2006

Introduction to Programming

Data Types
Every field has a type specified for it. Each parameter has a type specified for it. So far have met types: int e.g. 50, -3, 0, 129

String e.g. green or Bill or 129 (The quotes are very important)
boolean -- only two possible values: true and false NB: every class also defines a type!

Autumn 2006

Introduction to Programming

Strings and Numbers, etc


int 5 23
five

red

String 129 23 red

one hundred and twenty nine

Autumn 2006

Introduction to Programming

Observations
Many instances can be created from a single class An object has attributes: values stored in fields. The class defines what fields an object has, but each object stores its own set of values the class defines what methods each object (instance) has The class defines special methods to create objects (constructors)
Autumn 2006 Introduction to Programming

Example: two circle objects

Autumn 2006

Introduction to Programming

Example: Objects and State

Autumn 2006

Introduction to Programming

Picture Demo

Objects as values of fields

Autumn 2006

Introduction to Programming

Objects as Values
A field can have an object as its value
The type of such a field is the class of the objects it can take as values

Autumn 2006

Introduction to Programming

Exercise
Read Chapter 1 of Objects First with Java, and do all the exercises. Help will be given in exercise classes in week 2. Spend as much time as is necessary Try and finish before second lab class If you find this easy, read ahead in book!!!
Autumn 2006 Introduction to Programming

Source Code
Each class has source code (Java code) associated with it that defines its details (fields and methods). Source code must be compiled before it can be executed (run)
To create objects (instances of the class) To interact with objects
Autumn 2006 Introduction to Programming

What is Compilation?
Compilation is the process whereby one computer language is translated into another (usually simpler and more low-level i.e. machine-orientated) language.
The program that does the translation is known as a compiler.

Traditionally, programs in a high-level computer language (e.g. Pascal, C, Lisp) are compiled into assembly language (essentially machine code)
Java programs are translated into an intermediate language known as Java Virtual Machine (VM) code.
This VM code is then normally interpreted in order to run the program.
Autumn 2006 Introduction to Programming

What is Interpretation?
A computer can only directly execute programs written in its own machine language.

For many languages, the compiler translates the highlevel language program into machine language which can then be executed by the computer
In the case of Java, the compiler produces intermediate VM code i.e. not actual machine language The VM interpreter is a program (running on the machine) which carries out the intended actions of the VM instructions.
Autumn 2006 Introduction to Programming

An Analogy
When you see (256378 + 5674543) you do not execute this directly in your head. Instead, you follow a procedure (algorithm) for long addition, using pen and paper. In other words you interpret the high-level instruction (add these two numbers together) in order to get the result

Autumn 2006

Introduction to Programming

Source code Edit

The EditCompileRun Process

Modified source

Compile Errors? no Run Errors? no Finished yes yes

Compile Time Errors


Found by the compiler i.e. you get error messages when compiling
Syntax errors e.g. missing bracket, or semi-colon, and other illegal sequences of symbols. Type errors e.g. using an int where a String is expected.
Autumn 2006 Introduction to Programming

Run-Time Errors
Errors that happen when you run a successfully compile program. There are two kinds:

1.

Those that give a run-time error message because while your program is running it does something illegal (e.g.
dividing something by zero trying to access a non-existent object.

2.

Those that give no error message, but your program simply does not do what you want!
These are the hardest kinds of error to track down.
Introduction to Programming

Autumn 2006

Advice
All programmers make errors, even experienced ones
Do not panic! Do not get depressed!

Try to treat errors as a challenge


It is an absolutely fundamental part of the programming process to find and fix errors in programs (debugging) As you get more experienced, the easy bugs get easy to fix!

I doubt if a single large piece of software exists which is error free (bug free)

Autumn 2006

Introduction to Programming

Lab Class Demo


Return values Objects can be parameters

Autumn 2006

Introduction to Programming

Return values

Methods may return a result via a return value.

Autumn 2006

Introduction to Programming

Objects as Parameters
A parameter can be given an object as its value The type of such a parameter is the class of the objects it can be given (passed) as its value

Autumn 2006

Introduction to Programming

Exercise
Read Chapter 1 of Objects First with Java, and do all the exercises. Help will be given in exercise classes Try and finish before your second lab class If you find this easy, read ahead in book!!!

Autumn 2006

Introduction to Programming

You might also like