Chapter 6.2

7

Click here to load reader

Transcript of Chapter 6.2

Page 1: Chapter 6.2

DEFINING OBJECT, CLASS,

ATTRIBUTES & METHODS

Chapter 6.2:

Page 2: Chapter 6.2

Objects

Object-oriented programs use objects, which

represent real world objects.

A real world object is a thing, both tangible and intangible.

An object has:

state (it has various properties, which might change)

behaviour (it can do things and can have things done to it)

Page 3: Chapter 6.2

Objects

OOProgramming software

objects

Software objects have state

Instance Variable/Data/Field

Software objects have behaviour

Method

Object’s

members

Page 4: Chapter 6.2

Objects interactions

OOP also involves interactions between objects through calling (invoking) of methods.

Method CallsThe OilSensor object calls the warning() method

of the Controller, which then invokes the OilLight

TurnOn() method.

Page 5: Chapter 6.2

Method can Return Values

Return value: A result that the method has

computed and returns it to the caller

Can returns 0 or 1 value

Scanner s = new Scanner(System.in);

int num = s.nextInt();

Page 6: Chapter 6.2

Classes and Objects

To create an object , we MUST provide a definition/description for it

A class is a description/blue print of a kind of object

It does not by itself create any objectsHow to create an object ?

An object is called an instance of a

class

Object instantiation – process of creating an

object

Page 7: Chapter 6.2

Example of Java class:

The String Class