Overloading dan Overriding Kotlin
Summary
TLDRThis video discusses the key differences between method overloading and method overriding in object-oriented programming. Method overloading occurs when a class has multiple methods with the same name but different parameters, allowing flexibility in input types. In contrast, method overriding involves a subclass using the same method name as its parent class, but altering its behavior. The video explains these concepts with practical code examples, showcasing how method signatures and inheritance affect functionality. It also hints at future discussions on access modifiers, like private and protected, to deepen understanding.
Takeaways
- 📚 Overloading allows methods with the same name in a class to have different parameters (e.g., different data types or number of parameters).
- 🔢 Overloading works by differentiating methods based on the number and type of input parameters, even if they share the same name.
- 🔄 Overriding occurs when a subclass has a method with the same name and parameters as its parent class, but it provides its own implementation.
- 📏 The script explains overloading using a 'Rectangle' class example, where multiple 'calculate' methods take different parameter types (integer vs. double).
- 💡 Overloading is flexible, allowing methods with the same name but different input types or parameter counts, as long as they don't create ambiguity.
- 🚫 Two methods cannot have the same name and the same number/type of parameters in the same class because it would cause a conflict.
- 🧱 The script illustrates overriding using a 'Block' class that inherits from the 'Rectangle' class, where the child class redefines a 'display' method.
- ⚙️ The 'super' keyword is used to call the parent class's constructor in the child class when overriding.
- 🔍 When overriding, if a method exists in both the parent and child classes, the child class's version will be called unless specified otherwise.
- 🔑 The script also mentions that access modifiers like 'open', 'private', and 'protected' affect method visibility, which will be explained later.
Q & A
What is method overloading?
-Method overloading is a feature in object-oriented programming where a class has multiple methods with the same name but different parameters (different number, type, or order of parameters). The method to be executed is determined by the argument list during the call.
What is method overriding?
-Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its parent class. The method in the child class must have the same name, return type, and parameters as the method in the parent class.
What is the main difference between method overloading and method overriding?
-The main difference is that overloading happens within a single class and deals with multiple methods having the same name but different parameters. Overriding, on the other hand, involves a child class redefining a method that is already defined in its parent class, keeping the same name and parameters.
Can method overloading have different return types?
-Yes, method overloading can have different return types, as long as the parameter lists are different. However, the return type alone cannot be used to distinguish overloaded methods.
In the provided script, how does the `hitung` method demonstrate method overloading?
-The `hitung` method in the `PersegiPanjang` class is overloaded by creating multiple methods with the same name (`hitung`) but different parameter types (e.g., `int` vs. `double`) and different numbers of parameters.
How does the `display` method demonstrate method overriding in the script?
-The `display` method is defined in both the parent class (`PersegiPanjang`) and the child class (`Balok`). The method in the child class overrides the parent class method to provide a more specific implementation that includes additional attributes (e.g., height).
What happens if two overloaded methods have the same number and type of parameters?
-If two overloaded methods have the same number and type of parameters, the compiler will not be able to differentiate between them, causing an error. Overloaded methods must differ either in the number, order, or type of parameters.
Why is the `override` keyword used in method overriding?
-The `override` keyword explicitly indicates that a method is meant to override a method from its parent class. This helps in ensuring that the method signature matches the parent's method, reducing the risk of errors.
What is the purpose of the `super` keyword in the script?
-The `super` keyword is used to call the constructor of the parent class from the child class (`Balok`). This ensures that the parent class attributes (length and width) are initialized before additional attributes are set in the child class.
How does the script illustrate the difference between method overriding and method overloading?
-The script uses the `hitung` method to illustrate overloading by defining multiple `hitung` methods with different parameters within a single class. It demonstrates overriding by defining the `display` method in both the `PersegiPanjang` and `Balok` classes, showing how the child class method takes precedence when called from a child class instance.
Outlines
📚 Understanding Overloading
This paragraph explains the concept of method overloading in programming. Overloading occurs when a class has multiple methods with the same name but different parameters. The key difference between the methods is the number or type of parameters they accept. An example is given with a 'persegi panjang' (rectangle) class that has two 'hitung' (calculate) methods: one with integer parameters for length and width, and another with double parameters for the same. This allows the class to handle different data types and numbers of arguments without confusion.
🔄 Overloading with Different Parameters
The paragraph delves deeper into method overloading by illustrating how a method can be called with different parameter types or counts without causing errors. It uses the example of a 'hitung' method that can accept two parameters of the same type (integer or double) or three parameters (integer) to calculate volume. The explanation clarifies that overloading is about having the same method name with varying parameters, which can include different data types or a different number of parameters.
🔄 Overriding Methods in Inheritance
This section introduces method overriding, which is different from overloading. Overriding occurs when a subclass has a method with the same name as a method in its superclass. The example provided involves a 'balok' (cube) class inheriting from a 'persegi panjang' (rectangle) class. The 'balok' class has its own 'display' method, which is an override of the 'display' method in the superclass. The paragraph explains how the overridden method in the subclass is called instead of the one in the superclass when an object of the subclass is used.
🔄 Clarifying Overloading and Overriding
The final paragraph summarizes the differences between overloading and overriding. Overloading is about having methods with the same name but different parameters in the same class, while overriding is about a subclass having a method with the same name as a method in its superclass. The paragraph emphasizes the importance of understanding these concepts for proper class design and method implementation in object-oriented programming.
Mindmap
Keywords
💡Overloading
💡Overriding
💡Method
💡Class
💡Parameter
💡Return type
💡Superclass
💡Subclass
💡Constructor
💡Access modifier
Highlights
Overloading refers to having methods with the same name within a class but with different parameters, such as type or number of parameters.
In overloading, as long as method signatures differ, the methods can coexist within the same class without confusion.
Overloading is illustrated with a class that calculates the area of a rectangle using methods with different parameter types (e.g., integer vs double).
Overloading can also differ by the number of parameters, such as adding a third parameter to the method for height.
Methods in overloading can vary by the number of parameters or their data types but cannot have identical parameter signatures.
Override refers to redefining a method in a child class that already exists in the parent class with the same signature.
Overriding allows the child class to have its own specific implementation of a method that shares the same name and parameters as the parent class.
When a method is overridden in the child class, calling it from the child object will execute the child class's version of the method.
To override a method, the method signature (name and parameters) must be identical in both the parent and child classes.
If the parent class method needs to be called within the overridden method, the `super` keyword is used to access the parent class's implementation.
Overloading is restricted to methods in the same class, while overriding deals with methods across parent and child classes.
Overloading provides flexibility by allowing methods with the same name to process different data types or parameter counts.
Overriding supports polymorphism, allowing different implementations of the same method to behave differently in child classes.
The use of the `open` keyword allows methods in Kotlin to be overridden, ensuring access for overriding in child classes.
The main difference between overloading and overriding is that overloading involves methods in the same class, while overriding involves methods across a class hierarchy.
Transcripts
baik pada materi ini kita akan membahas
tentang over loading dan override apa
sih beda over loading dan overlight
override ya Jadi kalau over loading itu
Suatu kelas mempunyai nama method yang
sama antara satu dengan yang lainnya
namun yang membedakannya adalah
parameternya
nah sedangkan override over over reading
atau over reading
antara kelas parent dan Cell itu
mempunyai nama method yang sama itu
dikatakan override Nah untuk lebih
jelasnya akan saya coba jelaskan melalui
kode ya antara over loading dan overlay
misalkan saya akan membuat sebuah kelas
persegi panjang ya persegi panjang
nah di dalam kelas persegi panjang
terdapat method ya misalkan methodnya
adalah hitung
parameter masukannya adalah panjang ya
panjang namun tipenya adalah
integer kemudian lebar ya
lebar
tipenya integer
dan pengembaliannya juga integer
kemudian dia akan merekrut
panjang dikali lebar ya
Nah di sini method Pertamanya saya punya
nama metodenya hitung dengan parameter
masukan panjang tipenya integer lebar
Kemudian pada method yang kedua Saya
mempunyai nama yang sama juga itu namun
yang membedakan di sini panjangnya
tipenya adalah double
kemudian lebar
tipenya double
kemudian dia akan mengembalikan double
juga
panjang kali lebar
Nah inilah yang dinamakan over loading
ya nama methodnya sama di dalam satu
kelas namun parameternya berbeda
oke
kurang ya
double lebar juga double ya
Kemudian
pada klasemen saya akan create objek ya
misalkan
= persegi panjang
kemudian saya akan Tampilkan ya
hasil luas persegi panjang
persegi panjang
ini adalah P hitung misalkan ini yang
tipenya integer
2,4 misalkan
nah Ketika saya Run maka dia akan
mengacu kepada parameter masukannya
yaitu integer kalau ini ya
panjangnya integer lebarnya juga integer
maka dia akan mengeksekusi
eh function atau method yang menghitung
masukannya adalah integer nah ini 8 ya 8
Nah kalau misalkan ini saya buat double
2 misalkan 3 sementara ini
4,5 misalkan ya 4,5
artinya dia mengacu yang ini
kalau yang tadi yang ini yang yang
sekarang ini Nah inilah yang dinamakan
over loading
atau misalkan Saya punya fun hitung juga
ya
namun dia tipenya misalkan integer namun
jumlah parameternya lebih banyak
misalkan itu boleh misalkan panjang
sama-sama integer nih panjangnya integer
kemudian lebar integer juga misalkan ya
nah kemudian ini tinggi
tinggi integer
ini jumlahnya 3 sementara ini 2 Nah
kalau dia sama nah itu yang nggak boleh
misalkan ini sama-sama dua dengan dengan
tipe data yang sama itu nggak bisa
Kenapa karena dia bakal bingung pilih
method yang ini atau metode ini namun
kalau misalkan ternyata walaupun ininya
sama-sama itu kemudian ininya tipe
datanya misalkan
integer juga jumlahnya 3 itu tidak
masalah yang jadi masalah adalah tipe
datanya sama di sini jumlahnya 2 di sini
2 tipe Datanya juga dua yaitu yang jadi
masalah tapi kalau dia berbeda misalkan
ini 2 ini 3 itu Nggak masalah contohnya
misalkan di sini tinggi ya tinggi saya
masukin tinggi
kemudian dia mengembalikan integer
paytren
panjang kali lebar kali tinggi Ya kali
tinggi
nah ini nggak masalah karena jumlahnya 3
walaupun tipe datanya sama sama
Ketika saya panggil misalkan hasil
hitung 2
5 kemudian 6
maka dia akan menghasilkan sebuah
rumusan
panjang kali lebar kali tinggi atau
volume ya
seperti ini
Nah kalau misalkan sama-sama dua ini
boleh ya boleh nih namun misalkan yang
lebarnya tipenya double Nah itu boleh
asalkan tidak boleh sama gitu nah ini
boleh nih seperti ini boleh
inilah dinamakan
overloading
misalkan
3
Misalkan seperti ini dia parameternya
sama-sama dua namun yang membedakan di
lebar Kalau yang ini integer Kalau yang
ini double nah ini nggak masalah
Nah inilah yang dinamakan
overloading ya jadi nama method yang
sama dalam satu kelas namun yang
membedakannya adalah parameter bisa
jumlah parameternya sama atau berbeda
namun tidak boleh sama
kalau sama dia bakal bingung kira-kira
pilih yang mana ya error kan Oke ini
penjelasan tentang over loading nah saya
akan Jelaskan tentang materi tentang
over reading over reading itu apa sih
Oke jadi over reading itu nama methodnya
antara kelas
Saya punya kelas
ini saya hapus dulu ya saya hapus
misalkan Saya punya kelas
balok ya balok
kemudian ini saya hapus aja ya saya
hapus
saya punya konstruktor
misalkan ini
Farm
Farel
kemudian partai
ya integer
kemudian saya punya konstruktor
panjang integer
lebar Eh ini belum tinggi ya Sorry
panjang sama lebar ya
lebar Ini tipenya integer
yang akan melakukan set pada variabel P
dan l ya
sama dengan
panjang
l = lebar
seperti ini kemudian saya punya method
misalkan display ya display display
display dia menampilkan
panjang
kemudian lebar
dollar l nah ini ya dia menampilkan
panjang dan lebar misalkan kemudian saya
punya kelas balok ya kemudian di sini
dia merupakan
Child ya dari persegi panjang
ini saya kasih Open ya
kemudian di sini Saya punya konstruktor
saya punya variabel tinggi ya
[Musik]
kemudian saya punya konstruktor
panjang
integer
lebar
integer dan tinggi ya
[Musik]
Ia merupakan super
dari panjang koma lebar ya
kemudian saya set t = tinggi
tinggi
nah pada kelas persegi panjang saya
punya method yang sama dengan kelas r
nya misalkan display
ini misalkan dia menampilkan panjang ya
ini saya samain aja ya Ini apa aja
saya copas
saya tambahkan tinggi
Nah di sini ada tanda merah nah ini
artinya kita harus menambahkan overade
di sini
tinggal
kemudian di kelas itu jangan lupa kita
tambahkan Open di sini
ya
Open makanya dia agak error ya karena
sudah saya tambahkan akses ininya open
nanti kita akan belajar tentang akses
modifier ya
ada private ada protect dan sebagai
nya
nanti kita akan belajar lebih detail apa
sih beda protect kemudian Open dan
sebagainya
nah ini methodnya sama nih sama-sama
display antara dengan si balok nah
Ketika saya klik objeknya misalkan Pak
Ardi ya kalau nih
ininya misalkan 1 5 7 misalkan Ketika
saya panggil method display display
maka yang dipanggil adalah
pada kelas balok ini kelas SI turunannya
eh ya ini ya display yang ini Tampilkan
ya
Nah kalau misalkan mau
ini yang ini maka dia akan manggil ke
kelas iparnya
atau misalkan ya yang saya rasa ini
misalkan
kelas si A misalkan daerah a =
persegi panjang
a titik display nah display yang ini dia
akan memanggil yang ini
panjang lebar ini oh belum saya masukkan
ya nilainya ya
ini display masukkannya
Oh ininya belum saya tambahkan ya satu
sama dua
maka yang dihasilkan adalah display pada
klasifikasi
nah ini saya ini display yang ini
panjang lebar nah beda kalau misalkan
kita balok ya balok
kalau balok
[Musik]
dia akan memanggil method
punya dia sendiri yang ini
panjang lebar tinggi
Nah itu
nah ini ya seperti ini
bisa lagi ya misalkan Saya punya kelas
8 ini misalkan pada kelas parent ya
hello
hello
Nah ini bisa kita panggil apa namanya
itu kelas si anaknya itu memanggil sih
method Ya methodnya itu bisa
Hello itu bisa nah namun kalau misalkan
nama methodnya sama yang bisa dipanggil
adalah
method pada kelas anak tersebut
contohnya Ini ya display
sama dia yang dipanggil Yang ini
itulah pengertian override dan
overheading ya
Silakan dicoba ya
coba
nah adanya display dan display 1 Nah
kalau display 1 ya berada pada kelas nah
kalau yang ini berarti dia manggil pada
kelas punyanya dia sendiri nah Silahkan
dicoba dan dipraktekkan antara
overloading dan override saya ulang Saya
ulangi lagi ya kalau over loading itu
mempunyai nama yang sama pada kelas yang
sama namun yang membedakannya adalah
parameter parameter masukannya
Nah kalau misalkan jumlahnya berbeda
misalkan ya itu Nggak masalah atau
misalkan tipe datanya walaupun jumlahnya
sama nih jumlah masukannya sama atau
namun tipe datanya berbeda itu nggak
masalah kalau kalau ini ya kalau over
overload
tapi kalau dia sama yang jadi masalah
Nah kalau override dia akan memanggil
pada kelas SI induknya eh kelasnya
nah kalau mau memanggil kelas induknya
berarti kita harus mengklik
nanti materi selanjutnya kita akan
belajar tentang
asses modifier itu apa sih apa sih
protektif dan sebagainya ya Silakan
dicoba dan dipraktekkan overloading dan
override
Посмотреть больше похожих видео
SUPER() in Python explained! 🔴
Inheritance in Python | Python Tutorials for Beginners #lec89
01 - [Classes] C++ Intermediate Programming Tutorial
#56 Python Tutorial for Beginners | Constructor in Inheritance
Java Tutorial: Access modifiers, getters & setters in Java
70+ Trickiest Core Java Interview Questions and Answers 😱
5.0 / 5 (0 votes)