Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Pertemuan 6 Object Oriented Programming Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2."— Presentation transcript:

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

2 2 Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Mengimplementasikan konsep OOP dalam bahasa pemrograman Java Membuat program Java dengan konsep OOP

3 3 Outline Materi Introduction Encapsulation Inheritance Polymorphism Interface Package

4 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

5 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

6 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

7 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

8 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

9 9 OOP Concept: Object (cont) A Software Object, that have state and behavior

10 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)

11 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

12 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 lowerGear  information from You to YourBicycle, the parameters needed by the method

13 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+attributeThe Bicycle class

14 14 OOP Concept: Class

15 15 Creating class //Filename: Point2D.java public 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

16 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

17 17 Class Access Level SpecifierClassPackageSubclassWorld Private No specifier protected public

18 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)

19 19 Inheritance Reusability Top down: –Being more specific Bottom Up: –Find similarity Java Keyword: extends

20 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()); }

21 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

22 22 Polymorphism-Example //Filename: Point2D.java public 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

23 23 // Point3D.java 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; } // overide method from class Point2D public String toString() { return super.toString() + “, z: “+ z; }

24 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() }

25 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

26 26 Interface - Example // IUpdate.java public interface IUpdate { public void UpdateProgress(int nPercent); } // Child.java, that called from Main class public class Child { IUpdate u; public void addEventListerner(IUpdate iu) { u = iu; } public void run() { for (int i=1; i<=100; i++) u.updateProgress(i); }

27 27 // Main.java public class Main implements IUpdate { Child child; public Main() // contructor { child = new Child(); // connect to child object child.addEventListener(this); // run child proses, and updating progress child.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();} }

28 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;

29 29 Packages - Example package 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


Download ppt "1 Pertemuan 6 Object Oriented Programming Matakuliah: T0053/Web Programming Tahun: 2006 Versi: 2."

Similar presentations


Ads by Google