1-kelasobjek

24
Object Oriented Programming

Upload: ita-destiny

Post on 03-Apr-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 1/24

Object Oriented

Programming

Page 2: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 2/24

Object Oriented Programming

Object

Class

Variables

instance variables

class variables

Methods

instance methods

class methods

Constructors

Method Overloading

Method Overriding

Encapsulation

Inheritance

Composition

Polymorphism

Abstraction

Interfaces

 Nested Classes

Access Modifiers

public, private, protected

Packages

Keywords

this, super, final, static

Page 3: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 3/24

In object-oriented programming, a computer program may be seen as a

collection of individual units, or objects, that act on each other.

It is opposite to a traditional programming in which a program may be

seen as a collection of functions, or simply as a list of instructions to the

computer.

Each object is capable of receiving messages, processing data, and sending

messages to other objects.

Each object can be viewed as an independent little machine or actor with a

distinct role or responsibility.

Page 4: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 4/24

Object

Page 5: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 5/24

Object-oriented programming focuses on the

development of reusable software components, called

objects.

An object is a building block 

which contains variables and methods.

Objects are key to understanding object oriented

technology.

You can look around and can see many examples of 

real-world objects: dog, car, table, chair, bicycle.

Page 6: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 6/24

All real world objects have two characteristics:

state and behavior

For example car have states

 (current gear, number of gears, color, number of wheels)

and behaviors

(braking, accelerating, slowing down, changing gears)

Page 7: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 7/24

Software objects are modeled after real-world objects and they

also have state and behavior.

A software object maintains its state in one or more variable.

A software object implements its behavior with methods.

variables

(state)

Software Object

methods

(behavior)

Page 8: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 8/24

So our car object look like the following figure.

10 mph

(speed)

5th gear

(currentgear)

red

(color)

Change

gear

brake

accelerate

Car Object

Page 9: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 9/24

Class

Page 10: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 10/24

In the real world, we often have many objects of the same

kind. For example, my car is just one of many cars in the

world.

Using object-oriented terminology, we can say that my car 

object is an instance of the class of objects known as cars.

Cars have state (4 gears, 1 engine, 4 wheels) and behavior 

(change gears, accelerate) in common. However, each car’s

state is independent and can be different from each other.

Page 11: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 11/24

When car manufacturers build cars they take

advantage of the fact that cars share common

characteristics, by building many cars from the same blueprint.

It would be very inefficient to produce a new

 blueprint for every individual car manufactured.

In object-oriented, it is also possible to have many

objects of the same kind that share characteristics.Classes provide the benefits of creating a template of 

objects.

Page 12: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 12/24

we can take advantage of the fact that objects

of the same kind are similar and we can create

a blueprint for those objects.

A template or blueprint of objects is called a

class.

“A class is a template or blueprint that defines thevariables and the methods common to all objects of 

a certain kind.”

Page 13: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 13/24

Change

gear

Brake

Number

of gears

Number of wheels

Car object

After you've created the car class, you can create

any number of car objects from the class. Eachobject gets its own copy of all the variables defined

in the class.

Page 14: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 14/24

Brake Brake

Change

gear Changegear

speed = 15 speed =10

Color =

red

Color =

blue

gears = 4 gears = 4

Your Car

These two car objects created from the car class.

My Car

Page 15: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 15/24

Creating

Classes

Page 16: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 16/24

class ClassName

{

variable 1;

variable 1;

 method1(){}

 method2(){}

}

Page 17: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 17/24

class Car

{

int gears;

int wheels;

 public void changeGear()

{}

}

Page 18: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 18/24

Creating

Objects

Page 19: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 19/24

You can create an object of class with the

following syntax: -

ClassName objVariable = new ClassName( );

So the car class object can be created as:

Car c = new Car( );

Page 20: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 20/24

The object creation statement has three parts.

Car c = new Car( );

Declaration Instantiation Initialization

1. Declaration

2. Instantiation

3. Initialization

Page 21: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 21/24

Declaration

You declare variables of int type as: -

int a;

You can say that a is a variable who can refer 

to any type of int data.

Classes in java are also types so you can

declare class type variable as: -

Car c;You can say that c is a variable who can refer 

to any type of Car .

Page 22: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 22/24

Declaring a variable do not create any object.

The code Car c; does not create a new car 

object, it just declare a variable named c that

will be used to refer to a Car object. The

reference is still empty until assigned with a

new keyword.The empty reference is called null reference

in java.

c

Page 23: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 23/24

Instantiation

The new operator instantiates a class by

allocating a memory for a new object. Thenew operator returns a reference to the object

it created and this reference is assigned to the

appropriate variable.

Car object

c

Page 24: 1-kelasobjek

7/28/2019 1-kelasobjek

http://slidepdf.com/reader/full/1-kelasobjek 24/24

Initialization

The object is always initialize by calling aconstructor.

A constructor is a special method which has a

same name as class and used to initialize the

variables of that object.

In this case the Car class object is initialized by calling the constructor of Car class Car( );