0% found this document useful (0 votes)
16 views48 pages

02 - Class and Object-MJS

informatika

Uploaded by

Rifqi Fauzani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views48 pages

02 - Class and Object-MJS

informatika

Uploaded by

Rifqi Fauzani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

CII3B4

Pemrograman Berorientasi Objek

Class and Object


CLASS
OOP
Represent the real world: Baby
Why use class?
Cont.
Cont.
Creating Class
Notes

Class name is capitalize


1 class = 1 file
Having main methods means
the class can be run
Writing class
Design Class
Start from concrete object then generalize

• Things that object know


• Things that object does
Example
Cont.
Cont.
Summary
a class is a definition of objects of the same kind

Name (or identity): identifies the class.

Variables (or attribute, state, field): contains the static attributes


of the class.

Methods (or behaviors, function, operation): contains the dynamic


behaviors of the class.
OBJECT
Making your first object

1.Write your class


2.Write tester (driver) class
3.In your tester, make an
object and access the object’s
variable and methods
Write class
class Dog{

int size;

String breed;

String name;

void bark(){

System.out.println(“Wof! Wof!”);

}
Write tester/driver class
class DogTest {
public static void main (String[] args){
// test will be in here
}
}
Write test
class DogTest {
public static void main (String[] args){
Dog d = new Dog(); // make object (term: instantiate)

d.size = 40; // set size of dog


d.bark(); //call bark methods
}
}
Let’s create baby
// create “myBaby” object from class Baby
Baby myBaby = new Baby();

myBaby.name = “Sarah”;

Two use of main
Test your real test
–It is possible to have main in each
class to test class itself
To launch/start your java Apps
–main is entry point
Class is not object
One class, many objects

Class used to construct objects (using new keyword)

Class is “blueprint” or “recipe” for object


Example
VARIABLE
OOP Characteristics
1.Everything is an object
2.A program is bunch of objects telling each
other what to do by sending messages
3.Each object has its own memory made up
of other object
4.Every object has a type
5.All object of particular type can receive
the same message
More compact definition
An Object has state, behavior and
identity
2 Rule for variables

1.Variable must have type


2.Variable must have a name
What variable does?
Variable is just a cup
A container. It hold something
Primitive type variable
int, long, double, boolean, char, short, byte, float
The actual values are stored in the variable
Primitives are small enough that they just fit into
the cup
Example
byte x = 7;
Reference type variable
Everything except primitives type: object, array
Objects are too big to fit in a variable (cup)
Cont.
There is actually no such thing as an object
variable
There's only an object reference variable
An object reference variable holds bits that
represent a way to access an object
It doesn't hold the object itself, but it holds
something like a pointer (but not pointer like C)
You manipulate object with reference
Although you treat everything as an object, the identifier
you manipulate is actually a “reference” to an object.

You might imagine a television (the object) and a remote


control (the reference).

As long as you’re holding this reference, you have a


connection to the television, but when someone says,
“Change the channel” or “Lower the volume,” what you’re
manipulating is the reference, which in turn modifies the
object.

If you want to move around the room and still control the
television, you take the remote/reference with you, not the
television.
Reference as remote
Example
Dog myDog = new Dog();
3 Steps Object creation

1.Declare reference variable


–Dog myDog;
2.Create an object
–new Dog()
3.Link object to reference
–Dog myDog = new Dog();
Where program lives
1. Register (inside processor)
2. The stack (in RAM but have direct support from
processor)
3. The heap (also in RAM, very flexible)
4. Constant storage (ROM)
3 things to remember
Instance variable and object live on heap
Local variable live on stack
Life and death of object
Book b = new Book();
Book c = new Book();
Cont.
Book d = c;
Cont.
c = b;
Cont.
Book b = new Book();
Book c = new Book();
Cont.
b = c;
Cont.
c = null;
Complex example
Ilustration (create animation)
Question?
THANK YOU

You might also like