Dalam gambaran dunia nyata, banyak objek yang berbeda tetapi punya kesamaan atau kemiripan tertentu. 

Contohnya mobil dengan motor memiliki banyak kesamaan karena objek tersebut merupakan kendaraan. Mobil merupakan kendaraan darat begitu juga dengan motor. Mungkin yang membedakan objek tersebut adalah jumlah roda dan kapasitas penumpang yang dapat ditampung. 
Sama halnya pada OOP, beberapa objek yang berbeda bisa saja memiliki kesamaan dalam hal tertentu. Di situlah konsep inheritance atau pewarisan harus diterapkan. Pewarisan dapat mencegah kita melakukan perulangan kode. Untuk lebih memahaminya lihatlah contoh bagan pada sebuah kelas berikut:
2020031216412445298e8b960399d06bf16eab880badd8.png
Pada bagan di atas kita dapat lihat class Car, Motorcycle, Plane, dan Helicopter memiliki banyak properti yang sama seperti lisencePlate, manufacture, dan engineActive. Kemudian memiliki beberapa method yang sama seperti startEngines(), info(), dan parking().
Jika kita ubah diagram class Car di atas menjadi sebuah kode maka kode tampak seperti ini:

  1. class Car {

  2.     constructor(licensePlate, manufacture, wheels) {

  3.         this.licensePlate = licensePlate;

  4.         this.manufacture = manufacture;

  5.         this.wheels = wheels;

  6.         this.engineActive = false;

  7.     }

  8.     

  9.     startEngines() {

  10.         console.log(`Mesin kendaraan ${this.licensePlate} dinyalakan!`);

  11.     }

  12.     

  13.     info() {

  14.         console.log(`Nomor Kendaraan: ${this.licensePlate}`);

  15.         console.log(`Manufacture: ${this.manufacture}`);

  16.         console.log(`Mesin: ${this.engineActive ? "Active": "Inactive"}`);

  17.     }

  18.     

  19.     droveOff() {

  20.         console.log(`Kendaraan ${this.licensePlate} melaju!`)

  21.     }

  22.     

  23.     openDoor() {

  24.         console.log(`Membuka pintu!`)

  25.     }

  26.     

  27.     parking() {

  28.         console.log(`Kendaraan ${this.licensePlate} parkir!`);

  29.     }

  30. }


Tidak ada masalah dengan kode tersebut, tetapi jika kita akan membuat kelas lainnya seperti Motorcycle, Plane, dan Helicopter maka kita harus menuliskan properti dan method yang sama secara berulang. 
Dengan teknik inheritance, kita bisa mengelompokkan properti dan method yang sama. Caranya dengan membuat sebuah kelas baru yang nantinya akan diturunkan sifatnya pada class lain:
2020031216453692b650a5c0e0b6c77a74227d408e5757.png
Ketika class Vehicle telah dibuat, kelas lainnya dapat melakukan extends pada kelas tersebut untuk mewarisi sifatnya. Dalam pewarisan, class Vehicle dapat disebut sebagai super atau parent class. Kelas yang mewarisi sifat dari parent class disebut dengan child class.
Pada JavaScript jika kita ingin mewariskan sifat class, lakukan dengan keyword extends seperti berikut:

  1. class ChildClass extends ParentClass {

  2.     

  3. }


Sebagai contoh mari kita buat class Vehicle yang nantinya akan kita gunakan sebagai parent class.

  1. class Vehicle {

  2.     constructor(licensePlate, manufacture) {

  3.         this.licensePlate = licensePlate;

  4.         this.manufacture = manufacture;

  5.         this.engineActive = false;

  6.     }

  7.     

  8.     startEngines() {

  9.         console.log(`Mesin kendaraan ${this.licensePlate} dinyalakan!`);

  10.     }

  11.     

  12.     info() {

  13.         console.log(`Nomor Kendaraan: ${this.licensePlate}`);

  14.         console.log(`Manufacture: ${this.manufacture}`);

  15.         console.log(`Mesin: ${this.engineActive ? "Active": "Inactive"}`);

  16.     }

  17.     

  18.     parking() {

  19.         console.log(`Kendaraan ${this.licensePlate} parkir!`);

  20.     }

  21. }


Kemudian kita bisa membuat class Car sebagai child class dari Vehicle.

  1. class Car extends Vehicle {

  2.    constructor(licensePlate, manufacture, wheels) {

  3.        super(licensePlate, manufacture);

  4.        this.wheels = wheels;

  5.    }

  6.  

  7.    droveOff() {

  8.      console.log(`Kendaraan ${this.licensePlate} melaju!`);

  9.    }

  10.  

  11.    openDoor() {

  12.      console.log(`Membuka pintu!`);

  13.    }

  14. }


Dengan begitu selain properti dan method yang terdapat di dalamnya, class Car juga dapat mengakses seluruh properti dan method yang terdapat pada class Vehicle.

  1. class Vehicle {

  2.    constructor(licensePlate, manufacture) {

  3.        this.licensePlate = licensePlate;

  4.        this.manufacture = manufacture;

  5.        this.engineActive = false;

  6.    }

  7.   

  8.    startEngines() {

  9.        console.log(`Mesin kendaraan ${this.licensePlate} dinyalakan!`);

  10.    }

  11.   

  12.    info() {

  13.        console.log(`Nomor Kendaraan: ${this.licensePlate}`);

  14.        console.log(`Manufacture: ${this.manufacture}`);

  15.        console.log(`Mesin: ${this.engineActive ? "Active": "Inactive"}`);

  16.    }

  17.   

  18.    parking() {

  19.        console.log(`Kendaraan ${this.licensePlate} parkir!`);

  20.    }

  21. }

  22.  

  23. class Car extends Vehicle {

  24.    constructor(licensePlate, manufacture, wheels) {

  25.        super(licensePlate, manufacture);

  26.        this.wheels = wheels;

  27.    }

  28.  

  29.    droveOff() {

  30.      console.log(`Kendaraan ${this.licensePlate} melaju!`);

  31.    }

  32.  

  33.    openDoor() {

  34.      console.log(`Membuka pintu!`);

  35.    }

  36. }

  37.  

  38. const car = new Car("H121S", "Honda", 4);

  39. car.startEngines();

  40.  

  41. /* output:

  42. Mesin kendaraan H121S dinyalakan!

  43. */


Oiya pada constructor class Car, kita melihat penggunaan super(), apa itu maksudnya? Keyword super digunakan untuk mengakses properti dan method yang ada pada induk class ketika berada pada child class. Jadi super(lisencePlate, manufacture) di atas berarti kita mengakses constructor dari parent class dan mengirimkan lisencePlate, dan manufacture sebagai data yang dibutuhkan olehnya agar objek (instance) Car berhasil dibuat.
Penggunaan super sangat berguna ketika kita hendak menjalankan method overriding pada method parent. Contohnya kita akan melakukan method overriding pada method info() dengan menambahkan informasi jumlah roda pada mobil, maka kita dapat melakukannya dengan seperti ini:

  1. class Car extends Vehicle {

  2.    constructor(licensePlate, manufacture, wheels) {

  3.        super(licensePlate, manufacture);

  4.        this.wheels = wheels;

  5.    }

  6.  

  7.    droveOff() {

  8.      console.log(`Kendaraan ${this.licensePlate} melaju!`);

  9.    }

  10.  

  11.    openDoor() {

  12.      console.log(`Membuka pintu!`);

  13.    }

  14.  

  15.    /* overriding method info dari parent class */

  16.    info() {

  17.      super.info();

  18.      console.log(`Jumlah roda: ${this.wheels}`);

  19.    }

  20. }

  21.  

  22. const johnCar = new Car("H121S", "Honda", 4);

  23. johnCar.info();

  24.  

  25. /* output:

  26. Nomor Kendaraan: H121S

  27. Manufacture: Honda

  28. Mesin: Inactive

  29. Jumlah roda: 4

  30. */


Dalam melakukan pewarisan kelas, tidak ada tingkatan yang membatasinya. Maksudnya, kita dapat mewariskan sifat kelas A pada kelas B, lalu kelas B mewarisi sifatnya kembali pada kelas C dan selanjutnya. Sama halnya dengan Nenek kita mewarisi sifatnya kepada orang tua kita kemudian orang tua kita mewarisi sifatnya kepada kita. 
Sehingga jika dilihat dari bagan sebelumnya, class tersebut masih bisa dikelompokkan kembali menjadi seperti ini:
20200312165044bad7c7034d9d728224594a2fde9a7693.png
Inheritance Dalam OOP Javascript (Parent Class & Child Class)
Bookmark

Inheritance Dalam OOP Javascript (Parent Class & Child Class)

Type:
Genre:
Status:
Country:
Keywords: Watch online Inheritance Dalam OOP Javascript (Parent Class & Child Class) bahasa Indonesia, Streaming anime Inheritance Dalam OOP Javascript (Parent Class & Child Class) sub indo, Mau nonton anime terbaik dengan subtitle Indonesia? Streaming anime Jepang dari anime Inheritance Dalam OOP Javascript (Parent Class & Child Class), website streaming anime subtitle indonesia dan nonton anime indo update setiap hari, TV Inheritance Dalam OOP Javascript (Parent Class & Child Class), Terbaru Terupdate dan Terlengkap dengan Kualitas 1080p, 720p. 480p, 360p Anime Sub Indo.

Comments

Comment Report Rule
Show Comments
9Anime Comment Policies & Guidelines
  1. Spoiling Content

    Definition

    “Anime Comment” is a spoiler-free section and is usually used for a general discussion about anime without spoiling any content.

    “Episode Comment” may contain spoilers for current or previous episodes. Contents from manga or future episodes are considered as spoilers.

    Hiding spoilers with spoiler tags is not allowed either.

    Consequences

    Minor Spoiler:-

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

    Major Spoiler:-

    1st Offense: Permanent Ban

  2. Diversion From Topic

    Definition

    The comment section should be about the anime on hand. Although references from other anime may be used, the core topic of the comments should be about the anime at hand.

    This rule also applies on advertising other streaming websites which results in an instant ban.

    This rule also applies to spam, posting comments like “first”, “second”, etc. are considered spam.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  3. Flame War

    Definition

    Exchange of angry or abusive messages between the comments. Any form of insulting is also considered as flame war.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  4. Posting Harmful Link

    Definition

    Post of hyperlinks for websites that may contain viruses or may harm your PC or device.

    Consequences

    1st Offense: Permanent Ban + Deletion of All Comments

  5. Posting NSFW Content

    Definition

    Posting contents that are not safe for work, such as pornographic images or video, ecchi anime, hentai, etc.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  6. Inappropriate Profile Picture

    Definition

    Using a profile picture that is not safe for work. Although minor ecchi or fanservice is acceptable.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  7. Use of Common Sense

    Definition

    If you think that you’ll get in trouble for what you’re about to do, don’t do it.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  8. Upvote Abusing

    Definition

    Creating multiple alternative accounts to upvote your own comments.

    Consequences

    1st Offense: Permanent Ban on All Accounts + Deletion of All Comments

  9. Downvote Abusing

    Definition

    Targeting a user and downvoting all his comments, or creating multiple accounts to downvote one or more comments.

    Consequences

    1st Offense: Permanent Ban on All Accounts

  10. Swearing

    Definition

    Swearing is fine, as long as it’s not directed towards anyone, even as a joke. It may be misinterpreted by a moderator.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

  11. Moderator Verdict

    Definition

    A moderator’s verdict is final and arguing with them will only cause further punishment. If you have a complaint against a staff member, or wish to file a ban appeal, go to the 9Anime Discord Server and post your issue in the #disqus-appeals channel.

    Consequences

    1st Offense: Warning + Temporary Ban (Ranging 1 day to 3 days)

    2nd Offense Within 30 Days of 1st Offense: Permanent Ban

Think you have been unfairly warned or banned?

If you believe that you’ve been unfairly banned or if you have a complaint against one or more Disqus Staff Members, you may post an appeal or complaint on 9Anime Discord Server.

STEPS

  1. Join 9Anime Discord Server: https://discord.gg/xxxxxxx
  2. Read the #rules channel and follow instructions. (Optional, but highly recommended)
  3. Head towards #disqus-appeals channel.
  4. Use the appropriate template to appeal a ban or file a complaint.