1
public class SuperClass{
public
SuperClass(){
System.out.println("Ini
konstruktor milik SuperClass");
}
public
void methodSuperClass(){
System.out.println("Ini
method milik SuperClass");
}
}
2
public class SubClass extends SuperClass{
public
SubClass(){
super();
}
public
void methodSubClass(){
super.methodSuperClass();
}
}
3
public class DemoInheritance{
public
static void main(String[] args){
SubClass
sc=new SubClass();
sc.methodSubClass();
}
}
4
public class Persegi{
private
double panjang; //atribut
yang di sembunyikan
private
double lebar; //atribut
yang di sembunyikan
private
double tinggi; //atribut
yang di sembunyikan
public
Persegi(){
panjang=0;
lebar=0;
}
private
double luas(double p,double l){
return
p*l;
}
public
void setPanjang(double panjang1){
this.panjang=panjang1;
}
public
void setLebar(double lebar1){
this.lebar=lebar1;
}
public
double getPanjang(){
return
panjang;
}
public
double getLebar(){
return
lebar;
}
public
double getLuas(){
return
luas(panjang, lebar);
}
}
5
public class MainPersegi{
public
static void main(String[] args){
Persegi
pg=new Persegi();
pg.setPanjang(10);
pg.setLebar(20);
System.out.println("Panjang
: "+pg.getPanjang());
System.out.println("Lebar
: "+pg.getLebar());
System.out.println("Luas
: "+pg.getLuas());
}
}
6
abstract class Karyawan{
int
gaji;
Karyawan(){
gaji=1000000;
}
int
getGaji(){
return
gaji;
}
abstract
void naikGaji();
}
7
class Manager extends Karyawan{
Manager(){
gaji
+=2000000;
}
void
naikGaji(){gaji+=3000000; }
}
8
class Receptionist extends Karyawan{
Receptionist(){
gaji+=1000000;
}
void
naikGaji(){gaji+=10000000; }
}
9
class Polymorphisme{
public
static void main(String[] args){
Karyawan
K[]=new Karyawan[3];
K[0]=new
Manager();
K[1]=new
Receptionist();
K[2]=new
Manager();
for
(int i=0;i<3;i++)
System.out.println("Gaji
karyawan "+i+" adalah "+K[i].getGaji());
for(int
i=0;i<3;i++)
K[i].naikGaji();
for(int
i=0;i<3;i++)
System.out.println("Gaji
sekarang "+i+" adalah"+K[i].getGaji());}
Tidak ada komentar:
Posting Komentar