Pertemuan 6 Object Oriented Programming

29
1 Pertemuan 6 Object Oriented Programming Matakuliah : T0053/Web Programming Tahun : 2006 Versi : 2

description

Pertemuan 6 Object Oriented Programming. Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2. Learning Outcomes. Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mengimplementasikan konsep OOP dalam bahasa pemrograman Java Membuat program Java dengan konsep OOP. - PowerPoint PPT Presentation

Transcript of Pertemuan 6 Object Oriented Programming

Page 1: Pertemuan 6 Object Oriented Programming

1

Pertemuan 6Object Oriented Programming

Matakuliah : T0053/Web Programming

Tahun : 2006

Versi : 2

Page 2: Pertemuan 6 Object Oriented Programming

2

Learning Outcomes

Pada akhir pertemuan ini, diharapkan mahasiswa

akan mampu :

• Mengimplementasikan konsep OOP dalam bahasa pemrograman Java

• Membuat program Java dengan konsep OOP

Page 3: Pertemuan 6 Object Oriented Programming

3

Outline Materi

• Introduction

• Encapsulation

• Inheritance

• Polymorphism

• Interface

• Package

Page 4: Pertemuan 6 Object Oriented Programming

4

Procedural versus OOP

• Procedural programming language– C is an example– Action-oriented– Functions are units of programming

• Object-oriented programming language– Java is an example– Object-oriented– Classes are units of programming

• Functions, or methods, are encapsulated in classes

Page 5: Pertemuan 6 Object Oriented Programming

5

OOP In Java

• Every java program implement all OOP Concept:– Encapsulation: all java program must reside in

class, no class, no program– Inheritance: all class have a superclass, if not

mentioned, it’s automatically subclassing from Object class

– Polymorphism: all method are polymorphic in default

Page 6: Pertemuan 6 Object Oriented Programming

6

Encapsulation

• “Packaging an object’s variables within protective custody of its methods”

• Advantages:– Modularity– Information Hiding: object has a public

interface for communicate to others, so it can maintain private information and method that can be changed any time without affecting the communication

• Java Keyword: class

Page 7: Pertemuan 6 Object Oriented Programming

7

OOP Concept: Object

• Real world objects share 2 characteristics:– State

• Dogs have state: name, color, breed, hungry• Bicycles have state: current gear, current pedal

cadence, two wheels, number of gears)

– Behavior• Dogs have behavior: barking, fetching, wagging

tail)• Bycyles have behavior: braking, accelarating,

slowing down, changing gears

Page 8: Pertemuan 6 Object Oriented Programming

8

OOP Concept: Object (cont)

• Software objects are modeled after real-world objects in that they too have state and behavior

• Software object:– State variables– Behavior methods, that is a function

(subroutine)– Methods and variables is associated with an

object, that is reside in object

Page 9: Pertemuan 6 Object Oriented Programming

9

OOP Concept: Object (cont)

A Software Object, that have state and behavior

Page 10: Pertemuan 6 Object Oriented Programming

10

OOP Concept: Object (cont)

• Bicycle modeled as a software object:– 10 mph, 90 rpm, 5th known as instance variable

because they contain the state for a particular object– changeGears, brake, changeCadence known as

instance method, because they inspect or change the state of a particular bicycle instance (object)

Page 11: Pertemuan 6 Object Oriented Programming

11

OOP Concept: Message

• The bicycle is useful only when another object (you) interacts with it (pedal)

• Trough the interaction between objects, we can achieve higher-order functionality and more complex behavior

• Software object interact and communicate by sending message to another object

Page 12: Pertemuan 6 Object Oriented Programming

12

OOP Concept: Message

• Sometimes the receiver object need more information to do, this information called parameters

•You the sender object

•YourBicycle the receiver object

•ChangeGears the message, the method to perform

•lowerGearinformation from You to YourBicycle, the parameters needed by the method

Page 13: Pertemuan 6 Object Oriented Programming

13

OOP Concept: Class

A class is a blueprint, or prototype, that defines the variables and the methods common to all objects

of a certain kind

Class = method+attribute The Bicycle class

Page 14: Pertemuan 6 Object Oriented Programming

14

OOP Concept: Class

Page 15: Pertemuan 6 Object Oriented Programming

15

Creating class

//Filename: Point2D.javapublic class Point2D{

int x, y; // member variable

public Point2D() { x=0; y = 0;} public Point2D(int nx, int ny) {

setPoint(nx, ny); }

// setter method public setPoint(int nx, int ny) {

x = nx;y = ny;

}

// continue class Point declaration // getter method int getX() { return x; } int getY() { return y; }

public static void main(String[] args) { // create object p

Point2D p = new Point2D();

p.setPoint(1, 5); System.out.println(“x: “, p.getX()):

System.out.println(“y: “,p.getY()): }} // end of class declaration

Page 16: Pertemuan 6 Object Oriented Programming

16

Some Guidance for Creating class

• 1 file can contain >=1 class

• 1 file only can contains 1 public class

• Filename must be a public class name, beware of case sensitive, remember that Java is multiplatform

• Tips: It would be better to have every file for every class you created

Page 17: Pertemuan 6 Object Oriented Programming

17

Class Access Level

Specifier Class Package Subclass World

Private      

No specifier  

protected

public  

Page 18: Pertemuan 6 Object Oriented Programming

18

Methods

• No Default argument

• All parameter is passing by value– How can we passing by reference?

• Support function name overloading, ie: same name different function signature (type of argument, number of argument, order of argument)

Page 19: Pertemuan 6 Object Oriented Programming

19

Inheritance

• Reusability• Top down:

– Being more specific

• Bottom Up:– Find similarity

• Java Keyword: extends

Page 20: Pertemuan 6 Object Oriented Programming

20

Inheritance- Example

public class Point3D extends Point2D{ int z;

public Point3D(int nx, int ny, int nz) {

super(nx, ny); // called super class constructor z = nz;

} int getZ() { return z; } void setZ(int nz) { z = nz; }

public static void main(String[] args) { Point3D p = new Point3d(10, 10, 3); System.out.println(“(x, y, z): “+ p.getX() + “,” p.getY()

+ “,” + p.getZ()); }}

Page 21: Pertemuan 6 Object Oriented Programming

21

Polymorphism

• Many shapes, 1 function behave differently according to object instance

• “Late binding”, bind to instance not type

• In default all Java methods are “polymorphic”

• Use “final” keyword to make “early binding”, non polymorphic

Page 22: Pertemuan 6 Object Oriented Programming

22

Polymorphism-Example

//Filename: Point2D.javapublic class Point2D{

int x, y; // member variable

public Point2D() { x=0; y = 0;} public Point2D(int nx, int ny) {

setPoint(nx, ny); }

// setter method public setPoint(int nx, int ny) {

x = nx;y = ny;

}

// continue class Point deklaration // getter method int getX() { return x; } int getY() { return y; }

// overide method from class Object public String toString() { return “x: “+x “, y: “+y; }} // end of class declaration

Page 23: Pertemuan 6 Object Oriented Programming

23

// Point3D.javapublic class Point3D extends Point2D{ int z;

public Point3D(int nx, int ny, int nz) {

super(nx, ny); // called super class constructor

z = nz; }

int getZ() { return z; } void setZ(int nz) { z = nz; }

// overide method from class Point2D public String toString() { return super.toString() + “, z: “+ z; }

Page 24: Pertemuan 6 Object Oriented Programming

24

public static void main(String[] args) {

Point2D p1 = new Point2d(10, 10); Point2D p2 = new Point3d(10, 10, 3);

printObject(p1); // x: 10, y: 10 printObject(p2); // x: 10, y: 10, z: 3

}

static printObject(Object o) {

System.out.println(“Object content: “+ o); // that is called o.toString()

}}

Page 25: Pertemuan 6 Object Oriented Programming

25

Interface

• As container of abstract method

• Similar to class, except that all method are abstract, just contain declaration.

• As the way of solved some problem that need Multiple Inheritance feature

• As call back function

Page 26: Pertemuan 6 Object Oriented Programming

26

Interface - Example// IUpdate.javapublic interface IUpdate {

public void UpdateProgress(int nPercent);}

// Child.java, that called from Main classpublic class Child { IUpdate u;

public void addEventListerner(IUpdate iu) {

u = iu; }

public void run() { for (int i=1; i<=100; i++) u.updateProgress(i); }}

Page 27: Pertemuan 6 Object Oriented Programming

27

// Main.javapublic class Main implements IUpdate { Child child;

public Main() // contructor {

child = new Child();

// connect to child object child.addEventListener(this);

// run child proses, and updating progresschild.run();

}

// implement method declared in Iupdate interface public void UpdateProgres(int nPercent) { System.out.println(“Progess: “ +nPercent); }

public static void main(String[] args(){ new Main();}

}

Page 28: Pertemuan 6 Object Oriented Programming

28

Packages

• “A package is a collection of related classes and interfaces providing access protection and namespace management. ”

• 1 package is 1 subfolder in file system

• To organize file in project or library

• Keyword: package name;

Page 29: Pertemuan 6 Object Oriented Programming

29

Packages - Examplepackage com.binus.oop;

public class TestPackage {

public static void main(String[] args){

System.out.println(“Contoh penggunaan package”);}

}

• Catatan:– Program diatas diberi nama TestPackage.java– Program OOP.java terletak pada directory x:\

WebProg\OOP\com\binus\oop\TestPackage.java– Setting classpath SET CLASSPATH =

%CLASSPATH%\; x:\WebProg\OOP– Compile : X:\webProg\OOP\com\binus\oop>javac

TestPackage.java– Running: x:\>java com.binus.oop.TestPackage