Dalam dunia pemrograman objek, pewarisan atau inheritance adalah cara membuat sebuah class baru yang berasal dari sebuah class lama. Sebagai contoh, class Karyawan akan diwariskan ke class Dosen.
Inheritance, in the programming context, is the process by which one class can be derived from another. In Java, this is done via the extends keyword. When a class is derived from another, it automatically inherits all the features of its parent class.
This is a powerful tool, as it allows you to create complex classes by building on the work of others. In our example, the Karyawan class would be derived from the Employee class. This would give us all the features of Employee (such as name and salary), while also allowing us to add our own customizations (such as position and department).
There are a few different ways to implement inheritance in Java. In this article, we’ll be using the example of a class Employee being inherited by a class Professor.
First, we need to create our classes. The Employee class will be the parent or base class, and the Professor class will be the child or subclass. Here’s what the code for these classes looks like:
public class Employee {
}
public class Professor extends Employee {
}
In order to make the Professor class inherit the properties and methods of the Employee class, we use the extends keyword. This tells Java that the Professor class is a subclass of Employee. Any properties or methods that are defined in the Employee class will automatically be inherited by the Professor class.
We can also override existing methods and properties in the subclass. For example, we might want to change the salary of all Professors to be a little higher than that of Employees. We can do this by overriding the salary() method in the Professor class:
public class Professor extends Employee {
@Override public double salary() { // overrides salary() method from Employee class
return super.salary() + 1000; // adds 1000 to each Professor’s salary }
}
When you use inheritance in Java, you’re essentially borrowing code from one class and adding it to another. This can save you time and energy when you’re creating your own classes, as you don’t have to start from scratch. You can simply use the code that’s already been written, and tweak it to fit your own needs.
In addition, inheritance makes your code more efficient and organized. It allows different parts of the code to be handled by different classes, which makes the code easier to read and understand. This also helps prevent conflicts, as each class is responsible for its own specific tasks.
Overall, inheritance provides a number of benefits that can help improve the quality of your code. When used correctly, it can make coding faster and easier, while also reducing the chances of errors.
Using inheritance has its own drawbacks. Saat menuliskan kode Anda sebaiknya jadi berhati-hati agar tidak menghasilkan kesalahan seperti circular inheritance dan lain-lain. Selain itu, Anda harus ingat bahwa gaya coding Anda yang baik sangat penting meskipun Anda memakai inheritance dalam pemrograman.
Selain itu, adalah penting untuk dicatat bahwa inheritance digunakan hanya di situasi yang tepat – jika jenis warisan yang tepat dapat diimplementasikan dengan dekomposisi atau komposisi daripada pewarisan, maka gunakan yang terakhir. Ini bisa membuatnya lebih mudah untuk manajemen dan debugging kode saat Anda sedang membangun proyek.
Let’s say you have a class called Employees and a second class called Professors that you want to use. What you can do is have the Professors class inherit from the Employees class. That way, any attributes or methods of the Employees class will also exist in the Professors class—the Professors class just has some added features as well.
To implement this, you’d declare the Professors class like this: class Professor extends Employee. Then, any attributes or methods declared in the Employees will be available in the Professor’s class as well. So if you wanted to access a name attribute of an employee, you could just call it up with employeeName, even if it was declared in the parent (Employees) class.
It’s also important to note that when a child (or sub) class extends a parent (or super) class, it can override certain methods, adding its own functionality while still taking advantage of all of the functionality of its parent. So if you define an employeePay method in your Employees class, your Professor’s child classes can have their own implementation for the same method.
In Java, inheritance is a process by which one class inherits the properties of another class. In this way, the properties of the second class are available to the first class, and the first class can be modified to suit the needs of the programmer.
amnakhank22@gmail.com
+92 316 5544991