Thursday, October 15, 2015

How To Use Inheritance in Java Programming Language + video tutorial

How To Use Inheritance in Java Programming Language

The Following script is the simplest way to declaring and using Inheritance in Java programming language:

Class diagram:

Java program:

class Mahasiswa{ //mahasiswa = student
 String Nim; //student#
 String Nama; //name
 String Alamat; //address

 void setNim(String Nim){
  this.Nim = Nim;
 }

 String getNim(){
  return Nim;
 }

 void setNama(String Nama){
  this.Nama = Nama;
 }

 String getNama(){
  return Nama;
 }

 void setAlamat(String Alamat){
  this.Alamat = Alamat;
 }

 String getAlamat(){
  return Alamat;
 }
}

class MahasiswaReguler extends Mahasiswa{
 String Kelas = "Reguler";

 String getKelas(){
  return Kelas;
 }
}

class MahasiswaKaryawan extends Mahasiswa{
 String Kelas = "Karyawan";

 String getKelas(){
  return Kelas;
 }
}

class MahasiswaInheritance{
 public static void main(String args[]){
  MahasiswaReguler Ani = new MahasiswaReguler();
  MahasiswaKaryawan Adi = new MahasiswaKaryawan();

  Ani.setNim("001");
  Ani.setNama("Ani Sakura");
  Ani.setAlamat("Sumedang");
  System.out.println(Ani.getNim() +","+ Ani.getNama() +","+ Ani.getAlamat());

  Adi.setNim("002");
  Adi.setNama("Adi Naruto");
  Adi.setAlamat("Bandung");
  System.out.println(Adi.getNim() +","+ Adi.getNama() +","+ Adi.getAlamat());
 }
}

Output:
you can see the tutorial video here:


ShadowOfBdg© - All rights reserved. 

No comments:

Post a Comment