/*Contoh interface*/
public class Person{
private String name;
private String address;
private String city;
private int age;
private char gender;
/* Konstruktor */
/**
* Inisialisasi objek Person dengan nilai awal string kosong dan 0 untuk age
*/
protected Person() {
name = "";
address = "";
city = "";
age = 0;
gender = 'F';
}
/**
* Inisialisasi objek Person dengan nilai yang diberikan
*/
protected Person(String varName, String varAddress, String varCity, int varAge, char varGender) {
name = varName;
address = varAddress;
city = varCity;
age = varAge;
gender = varGender;
}
//setter
/**
* Mengubah nama
* @param varName nilai baru untuk nama
*/
protected void setName (String varName){
name = varName;
}
/**
* Mengubah Alamat
* @param varAddress nilai baru untuk alamat
*/
protected void setAddress (String varAddress) {
address = varAddress;
}
/**
* Mengubah Kota
* @param varCity nilai baru untuk kota
*/
protected void setCity (String varCity) {
city = varCity;
}
/**
* Mengubah Umur
* @param varAge nilai baru untuk umur
*/
protected void setAge (int varAge) {
age = varAge;
}
/**
* Mengubah jenis kelamin
* @param varGender nilai baru untuk jenis kelamin
*/
protected void setGender (char varGender) {
gender = varGender;
}
//getter
/**
* Mengambil nilai nama
* @return name nama
*/
protected String getName (){
return name;
}
/**
* Mengambil nilai alamat
* @return address address
*/
protected String getAddress (){
return address;
}
/**
* Mengambil nilai kota
* @return city city
*/
protected String getCity (){
return city;
}
/**
* Mengambil nilai umur
* @return name umur
*/
protected int getAge (){
return age;
}
/**
* Mengambil nilai jenis kelamin
* @return gender jenis kelamin
*/
protected int getGender (){
return gender;
}
/**
* Mengambil nilai Person
* @return str gabungan seluruh nilai atribut
*/
public String toString(){
String str =
"name : "+ name + "\n"+
"address : "+ address + "\n"+
"city : "+city + "\n"+
"age : "+age + "\n";
return str;
}
}
/**
* @author Frans Tuani Ryerson SIburian
*/
public class Instructor extends Person {
private String subject;
private String title;
private int salary;
/**
* Inisialisasi objek Instructor dengan nilai awal string kosong dan 0 untuk age dan salary
* Konstruktor secara default memanggil super()
*/
public Instructor() {
subject = "";
title = "";
salary = 0;
}
/**
* Inisialisasi objek Instructor dengan nilai yang diberikan
* Pemanggilan konstruktor berparameter dari kelas parent harus ditulis
* Jika tidak akan memanggil super()
*/
public Instructor (String varName, String varAddress,
String varCity, int varAge, char varGender,
String varSubject, String varTitle, int varSalary) {
super(varName, varAddress, varCity, varAge, varGender);
subject = varSubject;
title = varTitle;
salary = varSalary;
}
/**
* Mengubah mata kuliah
* @param varSubject nilai baru untuk mata kuliah
*/
public void setSubject (String varSubject) {
subject = varSubject;
}
/**
* Mengubah gelar
* @param varTitle nilai baru untuk gelar
*/
public void setTitle (String varTitle) {
title= varTitle;
}
/**
* Mengubah gaji
* @param varSalary nilai baru untuk gaji
*/
public void setSalary (int varSalary) {
salary = varSalary;
}
/**
* Mengambil nilai nama
* @return name nama
*/
public String getSubject () {
return subject;
}
/**
* Mengambil nilai gelar
* @return title gelar
*/
public String getTitle () {
return title;
}
/**
* Mengambil nilai gaji
* @return salary gaji
*/
public int getSalary () {
return salary;
}
/**
* Mengambil nilai Instructor
* @return str gabungan seluruh nilai atribut
*/
public String toString(){
String str =
super.toString()+"\n"+
"Subject : "+ subject + "\n"+
"Title : "+ title + "\n"+
"Salary : "+ salary;
return str;
}
}
public class DemoInheritance
{
public static void main(String []args)
{
Instructor instructor1 = new Instructor ("Umar Bakri", "Jl. Lurus No. 1", "Bandung", 30, 'L', "Strategi Algoritmik", "ST", 10000000);
System.out.println(instructor1.getName());
System.out.println(instructor1.getAge());
System.out.println(instructor1.getCity());
System.out.println(instructor1.getAge());
System.out.println(instructor1.getSubject());
System.out.println(instructor1.getTitle());
System.out.println(instructor1.getSalary());
System.out.println(instructor1);
instructor1.setName("Umar Bakri Tobing");
System.out.println(instructor1.toString());
}
}
mantap gan
BalasHapushttp://franssiburian.blogspot.com/