You are on page 1of 9

1.

File New Project

2. Buat nama Project


3. Enkapsulasi.java
4. Inheritance.java

5. Polymorph.java

6. Pertemuan2.java
SOURCE CODE:
ENKAPSULASI.JAVA
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pertemuan2;

/**
*
* @author firdaus.p.aji
*/
public class enkapsulasi {
private String nama;
private String nim;
private String alamat;

public void setNama(String nama){


this.nama = nama;
}

public void setNim(String nim){


this.nim = nim;
}

public void setAlamat(String alamat){


this.alamat = alamat;
}

public String getNama(){


return nama;
}

public String getNim(){


return nim;
}

public String getAlamat(){


return alamat;
}
}
INHERITANCE.JAVA
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pertemuan2;

/**
*
* @author firdaus.p.aji
*/
public class inheritance {
Persegi persegi = new Persegi();
Kubus kubus = new Kubus();

public void hasil(){


System.out.println("\nContoh tanpa inheritance : ");
persegi.luas();
System.out.println("\nContoh dengan inheritance : ");
kubus.volume();
}
}

class Persegi{
int p = 5;
int l = 2;

public int luas(){


int L = p * l;
System.out.println("Luas persegi : " + L);
return L;
}
}

class Kubus extends Persegi{


int t =10;

public int volume(){


int V = luas()*t;
System.out.println("Volume Kubus : "+ V);
return V;
}
}

POLYMORPH.JAVA
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pertemuan2;

/**
*
* @author firdaus.p.aji
*/
public class polymorph {
Keluarga keluarga = new Keluarga();
Bapak bapak = new Bapak();
Ibu ibu = new Ibu();
Anak anak = new Anak();
}

class Keluarga{
public void keluarga(){
System.out.println("Kita Keluarga");
}
}

class Bapak extends Keluarga{


@Override
public void keluarga(){
System.out.println("Ini Bapak");
}

public void keluarga(String bapak){


System.out.println("Nama Bapak : "+ bapak);
}
}

class Ibu extends Keluarga{


@Override
public void keluarga(){
System.out.println("Ini Ibu");
}

public void keluarga(String ibu){


System.out.println("Nama Ibu : "+ ibu);
}
}

class Anak extends Keluarga{


@Override
public void keluarga(){
System.out.println("Ini Anak");
}

public void keluarga(String anak){


System.out.println("Nama Anak : "+ anak);
}
}

PERTEMUAN2.JAVA
/*
* To change this license header, choose License Headers in Project
Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pertemuan2;

import java.util.Scanner;
/**
*
* @author firdaus.p.aji
*/
public class pertemuan2 {

public static void main(String][] args){

Scanner input = new Scanner(System.in);


boolean run = true;

while(run){
System.out.println("Aplikasi
OOP\n=======================");
System.out.println("Coba salah satu : ");
System.out.println("1. Enkapsulasi");
System.out.println("2. Inheritance");
System.out.println("3. Polymorphism");
System.out.println("4. Keluar");
System.out.println("\nPilihan");

int pilihan = input.nextInt();

switch(pilihan){
case 1:
enkapsulasi tes1 = new enkapsulasi();
input.nextLine();

System.out.println("\n\nAplikasi Data
Mahasiswa\n=======================");
System.out.println("Masukkan Nama : ");
tes1.setNama(input.nextLine());
System.out.println("Masukkan NIM : ");
tes1.setNim(input.nextLine());
System.out.println("Masukkan Alamat : ");
tes1.setAlamat(input.nextLine());

System.out.println("\n\nHasil input data : ");


System.out.println("Nama : " + tes1.getNama());
System.out.println("NIM : " + tes1.getNim());
System.out.println("Alamat : " + tes1.getAlamat());

run = false;
break;

case 2:
inheritance tes2 = new inheritance();
tes2.hasil();

run = false;
break;

case 3:
polymorph tes3 = new polymorph();

System.out.println("\nContoh Override : ");


tes3.keluarga.keluarga();
tes3.bapak.keluarga();
tes3.ibu.keluarga();
tes3.anak.keluarga();

System.out.println("\nContoh Overload : ");


tes3.bapak.keluarga("Supono");
tes3.ibu.keluarga("Sukinem");
tes3.anak.keluarga("Bob");

run = false;
break;

case 4:
System.out.println("Keluar");

run = false;
break;

default:
System.out.println("Ga ada di Pilihan");

run = false;
break;
}
}
System.exit(0);
}
}