Kali saya akan berbagi source code konsep inheritance atau pewarisan sifat pada objek menggunakan bahasa Java.
package konsepinheritance;
class A{
private int a;
public void Seta(int nilai){
a=nilai;
}
public int Geta(){
return a;
}
}
class B extends A{
private int b;
public void setb(int nilai){
b=nilai;
}
public int Getb(){
return b;
}
}
/**
*
* @author andi m
*/
public class KonsepInheritance {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
B obj=new B();
obj.Seta(100);
obj.setb(200);
System.out.println("nilai objek a:\t"+obj.Geta());
System.out.println("nilai objek b:\t"+obj.Getb());
}
}
package konsepinheritance;
class A{
private int a;
public void Seta(int nilai){
a=nilai;
}
public int Geta(){
return a;
}
}
class B extends A{
private int b;
public void setb(int nilai){
b=nilai;
}
public int Getb(){
return b;
}
}
/**
*
* @author andi m
*/
public class KonsepInheritance {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
B obj=new B();
obj.Seta(100);
obj.setb(200);
System.out.println("nilai objek a:\t"+obj.Geta());
System.out.println("nilai objek b:\t"+obj.Getb());
}
}
No comments