Anisafitriana89's Blog
Just another WordPress.com weblog

Hidding & Encapsulation

// Contoh program Information Hiding & Encapsulation
public class PersegiPanjang{
private double panjang; // attribute yg di hide
private double lebar; // attribute yg di hide
private double tinggi; // attribute yg di hide
public PersegiPanjang() {
panjang = 0;
lebar = 0;
}
private double luas(double p, double l) { // di encap
return p*l;
}
public void setPanjang(double panjang) {
this.panjang = panjang;
}
public void setLebar(double lebar) {
this.lebar = lebar;
}
public double getPanjang() {
return panjang;
}
public double getLebar() {
return lebar;
}
public double getLuas() {
return luas(panjang, lebar);
}
}
public class MainPersegiPanjang {
public static void main(String[] srgs) {
PersegiPanjang pp = new PersegiPanjang();
pp.setPanjang(10);
pp.setLebar(20);
System.out.println(”Panjang : ”+ pp.getPanjang());
System.out.println(”Lebar : ”+ pp.getLebar());
System.out.println(”Luas : ”+ pp.getLuas());
}
}

Output :

Panjang : 10

Lebar : 20

Luas : 200

No Responses to “Hidding & Encapsulation”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: