Inheritance Adalah konsep dimana sebuah claas(Ayah) dapat menurunkan property dan method yang dimiliki dan akan diwariskan kepada class lain (Anak). Jadi Class yang sudah mendapat waris bisa mengakses data dari class utama(Ayah).
Contoh
private int NIP;
private String nama;
public void set NIP(int, nip){
this, NIP= nip;
}
public int get NIP(){
return this , NIP;
}
publid void setNama(String nama){
this, nama = nama;
}
public String getNama(){
return this, nama;
}
}
int String Departemen;
Public Manager(int NIP, String nama, String departemen){
super(NIP,nama);
this.departemen = departemen;
}
public String getDepartemen(){
return departemen;
}
public void setDepartemen(String departemen) {
this.departemen = departemen;
}
}
private int NIP;
private String nama;
public Pegawai(int NIP, String nama){
this.NIP = NIP;
this.nama = nama;
}
public void setNip(int nip){
this.NIP = nip;
}
public int getNip(){
return this.NIP;
}
public void setNama(String nama){
this.nama = nama;
}
public String getNama(){
return this.nama;
}
}
private String departemen ;
public Manajer(int nip, String nama, String departemen) {
super(nip,nama);
this.departemen = departemen;
}
public String getDepartemen() {
return departemen;
}
public void setDepartemen(String departemen) {
this.departemen = departemen;
}
}
public static void main(String[] args) {
Manajer m = new Manajer(1, "Andi", "TI");
System.out.println("Nip lama : "+m.getNip());
System.out.println("Nama : "+m.getNama());
System.out.println("Departemen Sebelumnya : "+m.getDepartemen());
System.out.println("==========================================");
m.setNip(2103177001);
m.setNama("Ratna");
m.setDepartemen("Informatika");
System.out.println("Nip Baru : "+m.getNip());
System.out.println("Nama Baru: "+m.getNama());
System.out.println("Departemen Baru : "+m.getDepartemen());
}
}
Contoh
Buat Class Pegawai
public class Pegawai{private int NIP;
private String nama;
public void set NIP(int, nip){
this, NIP= nip;
}
public int get NIP(){
return this , NIP;
}
publid void setNama(String nama){
this, nama = nama;
}
public String getNama(){
return this, nama;
}
}
Buat Class Manager
public class Manager {int String Departemen;
Public Manager(int NIP, String nama, String departemen){
super(NIP,nama);
this.departemen = departemen;
}
public String getDepartemen(){
return departemen;
}
public void setDepartemen(String departemen) {
this.departemen = departemen;
}
}
Buat Pegawai.java
public class Pegawai {private int NIP;
private String nama;
public Pegawai(int NIP, String nama){
this.NIP = NIP;
this.nama = nama;
}
public void setNip(int nip){
this.NIP = nip;
}
public int getNip(){
return this.NIP;
}
public void setNama(String nama){
this.nama = nama;
}
public String getNama(){
return this.nama;
}
}
Buat Manager.java
class Manajer extends Pegawai {private String departemen ;
public Manajer(int nip, String nama, String departemen) {
super(nip,nama);
this.departemen = departemen;
}
public String getDepartemen() {
return departemen;
}
public void setDepartemen(String departemen) {
this.departemen = departemen;
}
}
Buat Departemen.java
public class Departemen {public static void main(String[] args) {
Manajer m = new Manajer(1, "Andi", "TI");
System.out.println("Nip lama : "+m.getNip());
System.out.println("Nama : "+m.getNama());
System.out.println("Departemen Sebelumnya : "+m.getDepartemen());
System.out.println("==========================================");
m.setNip(2103177001);
m.setNama("Ratna");
m.setDepartemen("Informatika");
System.out.println("Nip Baru : "+m.getNip());
System.out.println("Nama Baru: "+m.getNama());
System.out.println("Departemen Baru : "+m.getDepartemen());
}
}
Panggil Program Java menggunakan Cmd
EmoticonEmoticon