Archive for Juni, 2008

UAS OOP with JAVA

Akhirnya selesai juga, walaupun program yang aku buat masih sangat sederhana sekali. Terima kasih banyak aku ucapkan untuk teman-teman ku di Eresha. Untuk Pak Romi…Thanks atas pengantar membuat Programm di Java Net Bean.

/*
* HelloJFrame.java
*
* Created on 22 Juni 2008, 23:42
*/

/**
*
* @author Syaugi
*/
public class HelloJFrame extends javax.swing.JFrame {

/** Creates new form HelloJFrame */
public HelloJFrame() {
initComponents();
}

/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate=”collapsed” desc=”Generated Code”>
private void initComponents() {

jTextField1 = new javax.swing.JTextField();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
jLabel4 = new javax.swing.JLabel();
jTextField3 = new javax.swing.JTextField();
jTextField4 = new javax.swing.JTextField();
jLabel5 = new javax.swing.JLabel();
jLabel6 = new javax.swing.JLabel();
jTextField5 = new javax.swing.JTextField();
jLabel7 = new javax.swing.JLabel();
jButton2 = new javax.swing.JButton();
jButton3 = new javax.swing.JButton();
jTextField2 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
jLabel1 = new javax.swing.JLabel();
jLabel8 = new javax.swing.JLabel();
jLabel9 = new javax.swing.JLabel();
jLabel10 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jLabel12 = new javax.swing.JLabel();
jLabel13 = new javax.swing.JLabel();
jLabel14 = new javax.swing.JLabel();
jLabel15 = new javax.swing.JLabel();
jLabel16 = new javax.swing.JLabel();
jLabel17 = new javax.swing.JLabel();
jLabel18 = new javax.swing.JLabel();
jLabel19 = new javax.swing.JLabel();
jComboBox1 = new javax.swing.JComboBox();
jComboBox2 = new javax.swing.JComboBox();
jComboBox3 = new javax.swing.JComboBox();
jLabel20 = new javax.swing.JLabel();
jLabel21 = new javax.swing.JLabel();
jLabel22 = new javax.swing.JLabel();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel2.setText(“Jumlah Hari”);

jLabel3.setText(“Selisih Hari”);

jLabel4.setText(“Denda per Hari”);

jTextField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField3ActionPerformed(evt);
}
});

jTextField4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField4ActionPerformed(evt);
}
});

jLabel5.setText(“Harga Pinjam”);

jLabel6.setText(“Jumlah Yang Harus Dibayar”);

jTextField5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField5ActionPerformed(evt);
}
});

jButton2.setText(“Hitung”);
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});

jButton3.setText(“Hitung Selisih”);
jButton3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});

jButton1.setText(“Hitung Denda”);
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});

jLabel1.setText(“Masukkan Selisih Hari”);

jLabel8.setText(“Denda yang harus dibayar”);

jLabel11.setText(“Masukkan Denda yang dibayar”);

jLabel12.setText(“Kategori Jenis Buku yang Anda Pinjam:”);

jLabel13.setText(“Harga Peminjaman Buku:”);

jLabel14.setText(“Text Book”);

jLabel15.setText(“3000”);

jLabel16.setText(“Kamus”);

jLabel17.setText(“2000”);

jLabel18.setText(“Majalah”);

jLabel19.setText(“1000”);

jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { “Tutorial Java NetBean”, “36 Jam Belajar Java”, “Aplikasi Java”, “Java Tutorial” }));

jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { “English Dictionary”, “Kamus Bahasa Indonesia”, “Kamus Jepang-Indonesia” }));

jComboBox3.setModel(new javax.swing.DefaultComboBoxModel(new String[] { “CHIP Edisi Januari 2008”, “EASY Edisi February 2007”, “PC MEDIA Edisi February 2008” }));

jLabel20.setText(“Data Text Book”);

jLabel21.setText(“Data Daftar Kamus”);

jLabel22.setText(“Data Daftar Majalah”);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel12)
.addComponent(jLabel14, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel16)
.addComponent(jLabel18))
.addGap(87, 87, 87)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel19)
.addComponent(jLabel17)
.addComponent(jLabel15)
.addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(33, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(jComboBox1, 0, 147, Short.MAX_VALUE)
.addGap(23, 23, 23)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel22)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(19, 19, 19))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel20, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(61, 61, 61)
.addComponent(jLabel21)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel11, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
.addComponent(jLabel8)
.addComponent(jLabel3)
.addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
.addComponent(jLabel1)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jButton3))
.addComponent(jTextField4, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTextField1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel9, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
.addComponent(jTextField2, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE))
.addGap(6, 6, 6)
.addComponent(jButton1)))
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jButton2))
.addComponent(jLabel10, javax.swing.GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE))
.addGap(86, 86, 86))))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel12)
.addComponent(jLabel13))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel14)
.addComponent(jLabel15))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel16)
.addComponent(jLabel17))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel18)
.addComponent(jLabel19))
.addGap(18, 18, 18)
.addComponent(jLabel20))
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel21)
.addComponent(jLabel22)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jComboBox3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(37, 37, 37)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(6, 6, 6)
.addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(jButton3))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(13, 13, 13)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addGap(18, 18, 18)
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jLabel8)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel11)
.addComponent(jButton2))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel10, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING))
.addContainerGap(47, Short.MAX_VALUE))
);

pack();
}// </editor-fold>

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
double d=0.0, e=0.0;
try {
d=Double.parseDouble(jTextField4.getText());
e=Double.parseDouble(jTextField5.getText());
}
catch(NumberFormatException nfe){
nfe.printStackTrace();
}
int jumlah = (int) (d + e);
jLabel10.setText(jumlah+”Rupiah”);// TODO add your handling code here:
}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
int selisih = (int)((Double.parseDouble(jTextField1.getText()))-7);
jLabel7.setText(selisih + ” Hari”);// TODO add your handling code here:
}

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
double a=0.0, b=0.0;
try {
a=Double.parseDouble(jTextField3.getText());
b=Double.parseDouble(jTextField2.getText());
}
catch(NumberFormatException nfe){
nfe.printStackTrace();
}
int denda = (int) (a * b);
jLabel9.setText(denda + “Rupiah”);// TODO add your handling code here:
}

private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
// Display the string.
public void run() {
new HelloJFrame().setVisible(true);
}
});
}
// Variables declaration – do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JButton jButton3;
private javax.swing.JComboBox jComboBox1;
private javax.swing.JComboBox jComboBox2;
private javax.swing.JComboBox jComboBox3;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel10;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel12;
private javax.swing.JLabel jLabel13;
private javax.swing.JLabel jLabel14;
private javax.swing.JLabel jLabel15;
private javax.swing.JLabel jLabel16;
private javax.swing.JLabel jLabel17;
private javax.swing.JLabel jLabel18;
private javax.swing.JLabel jLabel19;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel20;
private javax.swing.JLabel jLabel21;
private javax.swing.JLabel jLabel22;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JLabel jLabel6;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel8;
private javax.swing.JLabel jLabel9;
private javax.swing.JTextField jTextField1;
private javax.swing.JTextField jTextField2;
private javax.swing.JTextField jTextField3;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
// End of variables declaration
}

Tampilan Awal

Tampilan Hasil

Tampilan Hasil

Iklan

Program Perpustakaan Sederhana

public class tugasPerpustakaan
{
public static void main(String[]args)
{
String dataInput;
int biaya, denda, jumlah;
System.out.println(“========================================”);
System.out.println(” SISTEM PERPUSTAKAAN “);
System.out.println(“========================================\n”);
System.out.println(“Pilih Kategori : \n”);
System.out.println(“1. Buku Teks”);
System.out.println(“2. Kamus”);
System.out.println(“3. Majalah\n”);
System.out.print(“Kategori yang dipilih [1-3]: “);
dataInput = InputKeyboard.inputString();
System.out.println(“”);

int pilih = Integer.valueOf(dataInput).intValue();
switch(pilih)
{
case 1 :
System.out.println(” DAFTAR BUKU \n”);
System.out.println(“1. Membuat Aplikasi Profesional dengan JAVA \n”);
System.out.println(“2. 36 Jam Mahir Visual Basic 6.0 \n”);
System.out.println(“3. Microsoft SQL Sever 2005 \n”);
System.out.println(“4. Membuat macam-macam kartu dengan Photoshop \n”);
System.out.println(“5. Jaringan SOHO \n”);
break;
case 2 :
System.out.println(” DAFTAR KAMUS\n”);
System.out.println(“1. English for Computer \n”);
System.out.println(“2. English for Kidz \n”);
System.out.println(“3. Belajar Bahasa Mandarin \n”);
System.out.println(“4. Kamus Bahasa Indonesia \n”);
System.out.println(“5. Kamus Bahasa Inggris \n”);
break;
case 3 :
System.out.println(” DAFTAR MAJALAH \n”);
System.out.println(“1. CHIP edisi Januari 2008 \n”);
System.out.println(“2. INFO KOMPUTER edisi Januari 2008 \n”);
System.out.println(“3. EASY edisi Januari 2008 \n”);
System.out.println(“4. DIGITAL IMAGING edisi Januari 2008 \n”);
System.out.println(“5. KOMPUTER AKTIF edisi Januari 2008 \n”);
break;
default:
System.out.println(“Silahkan Pilih Kategori Koleksi Buku, Kamus atau Majalah \n”);
break;
}
System.out.print(“Data Anda akan Pilih ? : “);
dataInput = InputKeyboard.inputString();
System.out.println(“Koleksi Yang Anda ingin pinjam adalah : “+dataInput+” \n”);
System.out.print(“Lama Pinjam (Jumlah Hari) : “);
dataInput = InputKeyboard.inputString();
System.out.println(“”);

int lama = Integer.valueOf(dataInput).intValue();
if(lama > 7){
if (pilih==1){
biaya = 2000;
}else if (pilih==2){
biaya = 3000;
}else{
biaya = 1000;
}
denda = (lama-7) * 500;
}else{
if (pilih==1){
biaya = 2000;
}else if (pilih==2){
biaya = 3000;
}else{
biaya = 1000;
}
denda =0;
}

System.out.println(“Biaya yang harus Anda Bayar : Rp. “+biaya);
System.out.println(“Denda : Rp. “+denda);
jumlah = biaya + denda;
System.out.println(“”);
System.out.println(“Total Biaya yang harus dibayar : Rp. “+jumlah);
System.out.println(“”);
}
}

Tampilan Program:

Tampilan Program Java Perpustakaan

Inheritance (Pewarisan/Penurunan)

INHERITANCE (PEWARISAN/PENURUNAN) SALAH SATU KARAKTERISTIK OBJECT ORIENTED PROGRAMMING (OOP)

Suatu bahasa Program dapat disebut Pemograman Berorientasi Objek (OOP) karena terdapat:

  1. Encapsulation (Pembungkusan)
  2. Polymorfisme (Perbedaan Bentuk)
  3. Inheritance (Pewarisan/Penurunan)

Encapsulation (Pembungkusan) adalah Variabel dan method dibungkus dalam suatu objek dibungkus agar terlindungi. Dimana Setelah dibungkus, barulah hak akses terhadapnya bisa ditentukan.

Polymorfisme (Perbedaan Bentuk) artinya objek dapat memiliki lebih dari satu bentuk.

Inheritance (Pewarisan/Penurunan) adalah Merupakan salah satu konsep dalam OOP dimana kemampuan suatu objek untuk mewarisi ciri-ciri objek lainnya.

Diantara ketiga karakteristik diatas saya akan lebih khusus membahas tentang Inheritance. Karena konsep Inheritance ini memudahkan Programmer Java dan dapat menggunakan ulang source code dari superclass yang telah ada ini yang biasa disebut dengan Reuse.

Subclass menyediakan state/behaviour yang spesifik yang membedakannya dengan superclass, meskipun atribut dan methodenya sama.

Programmer Java dapat mendefinisikan superclass khusus yang bersifat umum dan bisa terdapat pada superclass atau subclass yang disebut abstract class.

Dalam Inheritance juga dikenal dengan yang namanya Class. Dimana sebuah class bisa mewarisikan atribut dan methode-nya ke class yang lain. Class yang mewarisi sifat-sifat kepada class lainnya disebut dengan kelas induk (superclass). Class yang diwarisi sifat-sifat dari kelas lainya disebut dengan (kelas turunan (subclass).

Istilah dalam inheritance yang digunakan :

Extends
Keyword ini harus kita tambahkan pada definisi class yang menjadi subclass.

Superclass
Superclass digunakan untuk menunjukkan hirarki class yang berarti class dasar dari subclass/class anak.

Subclass
Subclass adalah class anak atau turunan secara hirarki dari superclass.

Konsep Multipe Inheritance dan Single Inheritance.

  • Suatu subclass bisa menjadi superclass bagi subclass yang lain. Ini berlaku di bahasa pemograman C dan Java.
  • Setiap class dapat memiliki lebih dari satu superclass, hanya berlaku dibahasa pemogramman C.
  • Setiap class hanya boleh satu superclass, hanya berlaku dibahasa pemogramman Java.

Contoh Inherintance (Pewarisan/Penurunan):

Disini saya akan mengambil objek meja: Dimana meja yang menjadi superclass atau kelas induk. Sedangkan yang menjadi turunannya (subclass) adalah meja kantor, dimana ada sifat meja telah diturunkan dan ditambah laci. Metode yang diberikan adalah penambahan panjang meja.

class Meja{
int panjang=0;
int lebar=0;
int tinggi=0;

//methode
void UbahPanjang(int TambahPanjang){
panjang=panjang+TambahPanjang;
System.out.println (“Tambah Panjang Meja= “+panjang);
}}

class MejaKantor extends Meja {
int JumlahLaci=0;
public void JumlahLaci(int BanyakLaci){
JumlahLaci=BanyakLaci;
System.out.println (“Jumlah Laci= “+JumlahLaci);
}
}

class MejaKantorBeraksi {
public static void main (String[]args){
MejaKantor mejaku= new MejaKantor();
mejaku.UbahPanjang(2);
mejaku.JumlahLaci(3);

}

}

Gambar Listing Program:

Program Inheritance

Hasil Program :

Hasil Program