Contoh Inheritance Class
Berikut Source Code nya
Inheritance
/**
* @(#)inheritance_tugas.java
*
* inheritance_tugas application
*
* @Reinert Yosua Rumagit
* @32110122
* @version 1.00 2012/4/4
*/
import java.util.Scanner;
class barang
{
Scanner a=new Scanner(System.in);
String nama;
float harga;
long qty;
public void set_nama ()
{
System.out.print("masukan nama ");
nama=a.next();
}
public void set_harga ()
{
System.out.print("Masukan harga ");
harga=a.nextFloat();
}
public void set_qty ()
{
System.out.print("Masukan Kuantiti ");
qty=a.nextLong();
}
public String get_nama()
{
return (nama);
}
public float get_harga()
{
return (harga);
}
public long get_qty()
{
return (qty);
}
}
class elektronik extends barang
{
Scanner b=new Scanner(System.in);
String merk;
String model;
String Type;
long total;
public void set_merk ()
{
System.out.print("masukan merk ");
merk=b.next();
}
public void set_model ()
{
System.out.print("masukan model ");
model=b.next();
}
public void set_type ()
{
System.out.print("masukan type ");
Type=b.next();
}
public void set_total (long ttl)
{
total=ttl;
}
public String get_merk()
{
return(merk);
}
public String get_model()
{
return(model);
}
public String get_type()
{
return(Type);
}
public long get_total()
{
return(total);
}
public void display()
{
System.out.println("Nama = "+get_nama());
System.out.println("harga = "+get_harga());
System.out.println("qty = "+get_qty());
System.out.println("total = "+harga*qty);
System.out.println("merek = "+get_merk());
System.out.println("model = "+get_model());
System.out.println("type = "+get_type());
}
}
class sembako extends elektronik
{
Scanner c=new Scanner(System.in);
String jenis;
String satuan;
String daerah;
public void set_jenis ()
{
System.out.print("masukan jenis ");
jenis=c.next();
}
public void set_satuan ()
{
System.out.print("masukan satuan ");
satuan=c.next();
}
public void set_daerah ()
{
System.out.print("masukan daerah ");
daerah=c.next();
}
public String get_jenis()
{
return(jenis);
}
public String get_satuan()
{
return(satuan);
}
public String get_daerah()
{
return(daerah);
}
public void display()
{
System.out.println("jenis = "+get_jenis());
System.out.println("satuan = "+get_satuan());
System.out.println("daerah = "+get_daerah());
System.out.println();
}
}
public class inheritance_tugas {
public static void main(String[] args) {
elektronik listrik=new elektronik();
sembako pangan=new sembako();
listrik.set_nama();
listrik.set_harga();
listrik.set_qty();
listrik.set_merk();
listrik.set_model();
listrik.set_type();
pangan.set_jenis();
pangan.set_satuan();
pangan.set_daerah();
listrik.display();
pangan.display();
System.out.println();
}
}
* @(#)inheritance_tugas.java
*
* inheritance_tugas application
*
* @Reinert Yosua Rumagit
* @32110122
* @version 1.00 2012/4/4
*/
import java.util.Scanner;
class barang
{
Scanner a=new Scanner(System.in);
String nama;
float harga;
long qty;
public void set_nama ()
{
System.out.print("masukan nama ");
nama=a.next();
}
public void set_harga ()
{
System.out.print("Masukan harga ");
harga=a.nextFloat();
}
public void set_qty ()
{
System.out.print("Masukan Kuantiti ");
qty=a.nextLong();
}
public String get_nama()
{
return (nama);
}
public float get_harga()
{
return (harga);
}
public long get_qty()
{
return (qty);
}
}
class elektronik extends barang
{
Scanner b=new Scanner(System.in);
String merk;
String model;
String Type;
long total;
public void set_merk ()
{
System.out.print("masukan merk ");
merk=b.next();
}
public void set_model ()
{
System.out.print("masukan model ");
model=b.next();
}
public void set_type ()
{
System.out.print("masukan type ");
Type=b.next();
}
public void set_total (long ttl)
{
total=ttl;
}
public String get_merk()
{
return(merk);
}
public String get_model()
{
return(model);
}
public String get_type()
{
return(Type);
}
public long get_total()
{
return(total);
}
public void display()
{
System.out.println("Nama = "+get_nama());
System.out.println("harga = "+get_harga());
System.out.println("qty = "+get_qty());
System.out.println("total = "+harga*qty);
System.out.println("merek = "+get_merk());
System.out.println("model = "+get_model());
System.out.println("type = "+get_type());
}
}
class sembako extends elektronik
{
Scanner c=new Scanner(System.in);
String jenis;
String satuan;
String daerah;
public void set_jenis ()
{
System.out.print("masukan jenis ");
jenis=c.next();
}
public void set_satuan ()
{
System.out.print("masukan satuan ");
satuan=c.next();
}
public void set_daerah ()
{
System.out.print("masukan daerah ");
daerah=c.next();
}
public String get_jenis()
{
return(jenis);
}
public String get_satuan()
{
return(satuan);
}
public String get_daerah()
{
return(daerah);
}
public void display()
{
System.out.println("jenis = "+get_jenis());
System.out.println("satuan = "+get_satuan());
System.out.println("daerah = "+get_daerah());
System.out.println();
}
}
public class inheritance_tugas {
public static void main(String[] args) {
elektronik listrik=new elektronik();
sembako pangan=new sembako();
listrik.set_nama();
listrik.set_harga();
listrik.set_qty();
listrik.set_merk();
listrik.set_model();
listrik.set_type();
pangan.set_jenis();
pangan.set_satuan();
pangan.set_daerah();
listrik.display();
pangan.display();
System.out.println();
}
}