Chapter 6.2
Click here to load reader
Transcript of Chapter 6.2
DEFINING OBJECT, CLASS,
ATTRIBUTES & METHODS
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)
Objects
OOProgramming software
objects
Software objects have state
Instance Variable/Data/Field
Software objects have behaviour
Method
Object’s
members
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.
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();
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
Example of Java class:
The String Class