Inheritance

Inheritance itu proses pewarisan. contoh : Sebuah objek bisa memiliki atribut maupun fungsi.

Jika buah apel adalah objek, warna merah adalah atribut yang dimilikinya.

Jika mobil adalah objek, maka jumlah rodanya adalah atributnya. Sedangkan "bisa menyala" adalah fungsi yang dimiliki oleh objek mobil itu.

class Person {
    constructor(name) {
        this.name = name;
    }

    walk() {
        console.log("walk")
    }
}

class Teacher extends Person {
    constructor(name, degree) {
        super(name);
        this.degree = degree;
    }

    teach() {
        console.log("tech")
    }
}

const teacher = new Teacher("Rajif", "S.Kom");
console.log(teacher)

Last updated

Was this helpful?