SHARE
TWEET

Polymorphism

luqman0611 Feb 21st, 2020 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. pemangilan class parent denga value kelas turunannya
  2.  
  3. package com.company;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.     // write your code
  9.         Mobil mobil = new Truck(); // Memanggil method dengan value anak turunan nya
  10.         mobil.setMerek("Toyota");
  11.        System.out.println("Merek Mobil : " + mobil.getMerek()); mobil.jenisMObil();
  12.  
  13.        Sedan sedan = new Sedan();
  14.        sedan.sedan(sedan);
  15.  
  16.  
  17.     }
  18. }
  19.  
  20. ================
  21. package com.company;
  22.  
  23. import java.awt.*;
  24.  
  25. public class Sedan extends Mobil {
  26.     public void sedan (Mobil mobil){
  27.         setTipe("LG200");
  28.         setMerek("Honda");
  29.         setJenisMobil("Sedan");
  30.         setTahun(2012);
  31.  
  32.         System.out.println("======================");
  33.         System.out.println("Jenis Mobil : " + mobil.getJenisMobil());
  34.         System.out.println("Merek Mobil : " +mobil.getMerek());
  35.         System.out.println("Tipe Mobil : " + mobil.getTipe());
  36.         System.out.println("Tahun Mobil : " +mobil.getTahun());
  37.  
  38.     }
  39. }
  40. =================================
  41. package com.company;
  42.  
  43.  
  44. public class Mobil {
  45.  
  46.     private String jenisMobil;
  47.     private String merek;
  48.     private String tipe;
  49.     private int tahun;
  50.  
  51.     public void jenisMObil(){
  52.         System.out.println("defaul");
  53.     }
  54.  
  55.     public String getJenisMobil() {
  56.         return jenisMobil;
  57.     }
  58.  
  59.     public void setJenisMobil(String jenisMobil) {
  60.         this.jenisMobil = jenisMobil;
  61.     }
  62.  
  63.     public String getMerek() {
  64.         return merek;
  65.     }
  66.  
  67.     public void setMerek(String merek) {
  68.         this.merek = merek;
  69.     }
  70.  
  71.     public String getTipe() {
  72.         return tipe;
  73.     }
  74.  
  75.     public void setTipe(String tipe) {
  76.         this.tipe = tipe;
  77.     }
  78.  
  79.     public int getTahun() {
  80.         return tahun;
  81.     }
  82.  
  83.     public void setTahun(int tahun) {
  84.         this.tahun = tahun;
  85.     }
  86. }
  87. ===================================
  88. package com.company;
  89.  
  90. public class Truck extends Mobil{
  91.  
  92.     public void jenisMObil(){
  93.         System.out.println("truck");
  94.     }
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top