Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Matakuliah
Tahun
Versi
: T0053/Web Programming
: 2006
:2
Pertemuan 6
Object Oriented Programming
1
Learning Outcomes
Pada akhir pertemuan ini, diharapkan mahasiswa
akan mampu :
• Mengimplementasikan konsep OOP
dalam bahasa pemrograman Java
• Membuat program Java dengan konsep
OOP
2
Outline Materi
•
•
•
•
•
•
Introduction
Encapsulation
Inheritance
Polymorphism
Interface
Package
3
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
4
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
5
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
6
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
7
OOP Concept: Object (cont)
• Software objects are modeled after realworld 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
8
OOP Concept: Object (cont)
A Software Object, that have state and behavior
9
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)
10
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
11
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
12
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
13
OOP Concept: Class
14
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
15
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
16
Class Access Level
Specifier
Class
Package
Subclass
Private
No specifier
protected
public
World
17
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)
18
Inheritance
• Reusability
• Top down:
– Being more specific
• Bottom Up:
– Find similarity
• Java Keyword:
extends
19
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());
}
}
20
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
21
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
22
// 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;
}
23
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()
}
}
24
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
25
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);
}
}
26
// 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();}
}
27
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;
28
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
29