Transcript Document

Programming with Objects and Classes

Liang, Chpt 6

Why do we call programs “classes”?

Our programs describe things containing

variables

and

methods.

public class nameRecord{ String[] names; public static void findName(String s) { … } public static void main (String[] args){ … } } When we run this program, we create an

object

(an

instance

) with these variables & methods. We can create more than one such object!

Since we can create more than one object, what our program specifies is a set or

class

of objects. This is why our program is called a

class

.

Introducing

objects

• Java is an

Object-oriented

programming language • An object is a programming entity which represents some coherent collection of properties (variables) and behaviour (methods) • Objects often correspond to things in the real world • We define a set of

objects

by creating a

class

A list of properties that define a class

From set theory: a class (a set) has

defining properties

so that

only

things with

all

those defining properties can be members of that class.

For example, the class square consists of all things with 4 equal length sides and 4 90° corners. These two properties define squares: if we create something with these two properties, it is a square.

radius getArea main Names findFame main Names findFame main Names findFame main Our program classes are defined by the list of variables that instances posesses, and the methods (abilities) they have.

For examples, the properties “has an array of Strings called names ”, “has the method findName ”, and “has the main method” define the class nameRecord : each time we create an object from that class, it will have these properties.

Classes and objects

• a

class

is a definition of a set of

Objects

• This definition lists the properties (

the variables and the methods

) which each object (each instance of the class) will have • A class is like an object factory. One class definition provides a blueprint for as many objects as you like.

This idea of classes and objects is very useful. For example, it will let us write our own data types to represent useful things (people, shapes, etc.) in the same way as the int, double, String classes represent those things.

the

circle

class

We might choose to create a class

circle

so that each object in that class will be a different circle.

Each circle should minimally store its own

radius

and should have the useful ability to tell us its own area.

Circle class name radius properties (instance variables) findArea behaviour (methods)

The

circle

class (first pass)

class Circle { double radius = 1.0; •

No main method!

•1 instance variable •1 method double findArea() { return radius * radius * 3.14159; } }

Declaring and Creating objects

• Objects must be declared and then created • Declaration looks a lot like declaring a normal variable: Circle c; Variable name for this circle Variable type • Declaration alone enough is not enough!

• We must also

create

a new circle object and put it in the variable: c = new Circle(); This tells java to set aside new space in memory for a Circle object (with the variables & methods that define the circle class).

Our

Using objects

Circle class has no it as a standalone program.

main method: therefore we can’t use How do we use it? We use our Circle class in another class which does have a main method. class Circle { double radius = 1.0; double findArea() { return radius*radius*3.14159; } } public class CircleTest { The CircleTest of the Circle public static void main(String[] args) { Circle c = new Circle(); System.out.println("Area is " +

c.findArea()

); } class creates and uses objects class } Here we call the findArea method in the Circle object c

Where to put the

Circle

class?

• We can define the

Circle

class in the same file as the

TestCircle

class • If so, only

TestCircle

needs to be declared

public

• public class TestCircle......

• class Circle..... // order makes no difference •

TestCircle

contains the

main

method • Alternatively, you can put

Circle

into a file called

Circle.java

• public class Circle.......

• You must declare the Circle class public when it is in a file of its own: to let the TestCircle class access it.

Constructors

• Problem: all our circles are identical (radius=1) • Answer: provide a

constructor

which is called when the object is created • The constructor can take the desired radius as a parameter and set the radius to that value Circle c = new Circle(5.0); Constructor called when new created; sets radius to 5.0

Circle is • The

constructor

is a special method defined in a class • Constructor

always

has the same name as class (eg Circle ) • Constructor method is called whenever new class member is created.

Two constructors for

Circle

class Circle { // instance variable (no default value given) double radius; double findArea() { return radius*radius*3.14159; } } // constructor taking one parameter Circle(double r){ radius = r; Circle c = new Circle(5.0); } calls this constructor // constructor taking no parameter Circle(){ radius = 1.0; Circle c = new Circle(); } calls this constructor

These constructor “methods” are used when creating Circle objects

// in main method of testCircle class: Circle c1 = new Circle(5.0); Circle c2 = new Circle(); Call 1-parameter Circle Circle constructor Call no-parameter constructor System.out.println("C1 has area " + c1.findArea()); System.out.println("C2 has area " + c2.findArea()); Call findArea method for circle c2 Call findArea method for circle c1

Some things about constructors

• The name of the constructor is the same as the class name (always) • Constructors do not have return types (not even

void

) • Different constructors have different parameter lists • Constructors are only called when an object is created

The default constructor

• If no constructor is defined, a default constructor is invoked • This sets instance variables to default values (numbers  0, objects  null, etc) • If we define a constructor which takes no parameters, this is now the default constructor ( Circle() )

Objects vs basic datatypes in memory

int i; Circle c; null ?

c i i = 1; c = new Circle(); 1 i c c: Circle radius = 1

Assignment with basic datatypes

int i = 3; int j = 2; i = j; Before After 3 2 i i j j 2 2

Assignment with object variables

Circle c1 = new Circle(); Circle c2 = new Circle(); c1 = c2; c1: Circle radius = 1 c1 c2: Circle radius = 1 c2

Assignment with object variables

Circle c1 = new Circle(); Circle c2 = new Circle(); c1 = c2; c1: Circle radius = 1 c1 c2: Circle radius = 1 c2

Garbage collection

• So what happens to the memory previously occupied by c1?

• The object variable

c1

so we can't use it.......

no longer refers to it, • The garbage collecter will come along and reclaim that memory • We should never have to worry about the garbage collector