Overloading dan Overriding Kotlin

RIKI AFRIANSYAH
11 Nov 202216:23

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

00:00

📚 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.

05:00

🔄 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.

10:01

🔄 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.

15:04

🔄 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

Overloading refers to a concept in object-oriented programming where multiple methods within the same class share the same name but differ in the parameters they take (e.g., type, number of parameters). In the script, the speaker illustrates overloading with the example of methods that calculate area but differ in the parameter types, such as using integers versus doubles.

💡Overriding

Overriding is when a subclass or child class provides a specific implementation of a method that is already defined in its parent class. The method in the subclass has the same name and parameters as in the parent class. In the video, overriding is shown through a 'display' method in both the 'balok' and 'persegi panjang' classes, where the subclass 'balok' overrides the parent class’s method.

💡Method

A method is a function defined within a class that performs a specific task. Methods can take parameters and return values. The script frequently discusses methods in terms of their role in both overloading and overriding, such as the 'hitung' method that calculates area, but with different parameter types.

💡Class

A class in object-oriented programming is a blueprint for creating objects, defining their attributes and behaviors through variables and methods. In the script, examples include 'persegi panjang' (rectangle) and 'balok' (cuboid) as classes that define methods like 'hitung' and 'display'.

💡Parameter

A parameter is a variable used in a method to accept input values. Parameters define how the method functions based on the input provided. In the script, parameters such as 'panjang' (length), 'lebar' (width), and 'tinggi' (height) are passed to the 'hitung' method to calculate area and volume, highlighting the role of different types of parameters in overloading.

💡Return type

The return type of a method specifies the type of value it will return after execution. For example, in the script, different return types like 'integer' and 'double' are shown in overloaded 'hitung' methods. The method returns the result of multiplying length and width, with the return type varying based on input types.

💡Superclass

A superclass is a parent class from which other classes (subclasses) inherit methods and attributes. In the script, 'persegi panjang' is a superclass, and 'balok' is its subclass, which inherits and overrides methods from 'persegi panjang', demonstrating the relationship between superclass and subclass.

💡Subclass

A subclass is a class that inherits from a superclass and can override or extend its behavior. The script describes 'balok' as a subclass of 'persegi panjang', overriding the 'display' method to include additional behavior like displaying the height, alongside the length and width.

💡Constructor

A constructor is a special method in a class used to initialize objects. It assigns initial values to the attributes of the object. The script mentions constructors in both 'persegi panjang' and 'balok' classes, where attributes like 'panjang' (length), 'lebar' (width), and 'tinggi' (height) are set when an object is created.

💡Access modifier

An access modifier defines the scope and visibility of a class, method, or variable. Examples include 'public', 'private', and 'protected'. The script mentions that understanding access modifiers like 'Open' will be important later, indicating the control of how methods or properties are accessed in different contexts.

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

play00:01

baik pada materi ini kita akan membahas

play00:05

tentang over loading dan override apa

play00:09

sih beda over loading dan overlight

play00:11

override ya Jadi kalau over loading itu

play00:15

Suatu kelas mempunyai nama method yang

play00:18

sama antara satu dengan yang lainnya

play00:20

namun yang membedakannya adalah

play00:22

parameternya

play00:25

nah sedangkan override over over reading

play00:28

atau over reading

play00:30

antara kelas parent dan Cell itu

play00:33

mempunyai nama method yang sama itu

play00:35

dikatakan override Nah untuk lebih

play00:38

jelasnya akan saya coba jelaskan melalui

play00:40

kode ya antara over loading dan overlay

play00:43

misalkan saya akan membuat sebuah kelas

play00:47

persegi panjang ya persegi panjang

play00:55

nah di dalam kelas persegi panjang

play00:58

terdapat method ya misalkan methodnya

play01:02

adalah hitung

play01:04

parameter masukannya adalah panjang ya

play01:06

panjang namun tipenya adalah

play01:09

integer kemudian lebar ya

play01:13

lebar

play01:15

tipenya integer

play01:18

dan pengembaliannya juga integer

play01:20

kemudian dia akan merekrut

play01:24

panjang dikali lebar ya

play01:28

Nah di sini method Pertamanya saya punya

play01:30

nama metodenya hitung dengan parameter

play01:33

masukan panjang tipenya integer lebar

play01:36

Kemudian pada method yang kedua Saya

play01:39

mempunyai nama yang sama juga itu namun

play01:42

yang membedakan di sini panjangnya

play01:45

tipenya adalah double

play01:47

kemudian lebar

play01:50

tipenya double

play01:53

kemudian dia akan mengembalikan double

play01:55

juga

play02:02

panjang kali lebar

play02:07

Nah inilah yang dinamakan over loading

play02:10

ya nama methodnya sama di dalam satu

play02:12

kelas namun parameternya berbeda

play02:15

oke

play02:19

kurang ya

play02:27

double lebar juga double ya

play02:31

Kemudian

play02:33

pada klasemen saya akan create objek ya

play02:36

misalkan

play02:38

= persegi panjang

play02:41

kemudian saya akan Tampilkan ya

play02:46

hasil luas persegi panjang

play02:50

persegi panjang

play03:05

ini adalah P hitung misalkan ini yang

play03:09

tipenya integer

play03:11

2,4 misalkan

play03:14

nah Ketika saya Run maka dia akan

play03:16

mengacu kepada parameter masukannya

play03:19

yaitu integer kalau ini ya

play03:22

panjangnya integer lebarnya juga integer

play03:26

maka dia akan mengeksekusi

play03:28

eh function atau method yang menghitung

play03:33

masukannya adalah integer nah ini 8 ya 8

play03:36

Nah kalau misalkan ini saya buat double

play03:40

2 misalkan 3 sementara ini

play03:44

4,5 misalkan ya 4,5

play03:52

artinya dia mengacu yang ini

play03:56

kalau yang tadi yang ini yang yang

play03:58

sekarang ini Nah inilah yang dinamakan

play04:01

over loading

play04:03

atau misalkan Saya punya fun hitung juga

play04:05

ya

play04:08

namun dia tipenya misalkan integer namun

play04:12

jumlah parameternya lebih banyak

play04:14

misalkan itu boleh misalkan panjang

play04:16

sama-sama integer nih panjangnya integer

play04:21

kemudian lebar integer juga misalkan ya

play04:28

nah kemudian ini tinggi

play04:33

tinggi integer

play04:35

ini jumlahnya 3 sementara ini 2 Nah

play04:38

kalau dia sama nah itu yang nggak boleh

play04:39

misalkan ini sama-sama dua dengan dengan

play04:42

tipe data yang sama itu nggak bisa

play04:44

Kenapa karena dia bakal bingung pilih

play04:46

method yang ini atau metode ini namun

play04:48

kalau misalkan ternyata walaupun ininya

play04:50

sama-sama itu kemudian ininya tipe

play04:53

datanya misalkan

play04:56

integer juga jumlahnya 3 itu tidak

play04:58

masalah yang jadi masalah adalah tipe

play05:00

datanya sama di sini jumlahnya 2 di sini

play05:03

2 tipe Datanya juga dua yaitu yang jadi

play05:05

masalah tapi kalau dia berbeda misalkan

play05:07

ini 2 ini 3 itu Nggak masalah contohnya

play05:10

misalkan di sini tinggi ya tinggi saya

play05:13

masukin tinggi

play05:20

kemudian dia mengembalikan integer

play05:24

paytren

play05:26

panjang kali lebar kali tinggi Ya kali

play05:31

tinggi

play05:32

nah ini nggak masalah karena jumlahnya 3

play05:35

walaupun tipe datanya sama sama

play05:39

Ketika saya panggil misalkan hasil

play05:43

hitung 2

play05:45

5 kemudian 6

play05:50

maka dia akan menghasilkan sebuah

play05:53

rumusan

play05:55

panjang kali lebar kali tinggi atau

play05:58

volume ya

play06:01

seperti ini

play06:04

Nah kalau misalkan sama-sama dua ini

play06:06

boleh ya boleh nih namun misalkan yang

play06:09

lebarnya tipenya double Nah itu boleh

play06:11

asalkan tidak boleh sama gitu nah ini

play06:15

boleh nih seperti ini boleh

play06:17

inilah dinamakan

play06:20

overloading

play06:27

misalkan

play06:31

3

play06:33

Misalkan seperti ini dia parameternya

play06:36

sama-sama dua namun yang membedakan di

play06:38

lebar Kalau yang ini integer Kalau yang

play06:40

ini double nah ini nggak masalah

play06:43

Nah inilah yang dinamakan

play06:46

overloading ya jadi nama method yang

play06:49

sama dalam satu kelas namun yang

play06:51

membedakannya adalah parameter bisa

play06:54

jumlah parameternya sama atau berbeda

play06:56

namun tidak boleh sama

play06:58

kalau sama dia bakal bingung kira-kira

play07:00

pilih yang mana ya error kan Oke ini

play07:03

penjelasan tentang over loading nah saya

play07:06

akan Jelaskan tentang materi tentang

play07:08

over reading over reading itu apa sih

play07:10

Oke jadi over reading itu nama methodnya

play07:14

antara kelas

play07:21

Saya punya kelas

play07:23

ini saya hapus dulu ya saya hapus

play07:27

misalkan Saya punya kelas

play07:33

balok ya balok

play07:39

kemudian ini saya hapus aja ya saya

play07:41

hapus

play07:45

saya punya konstruktor

play07:48

misalkan ini

play07:52

Farm

play07:58

Farel

play08:03

kemudian partai

play08:08

ya integer

play08:11

kemudian saya punya konstruktor

play08:14

panjang integer

play08:18

lebar Eh ini belum tinggi ya Sorry

play08:20

panjang sama lebar ya

play08:23

lebar Ini tipenya integer

play08:28

yang akan melakukan set pada variabel P

play08:30

dan l ya

play08:33

sama dengan

play08:36

panjang

play08:38

l = lebar

play08:43

seperti ini kemudian saya punya method

play08:50

misalkan display ya display display

play08:55

display dia menampilkan

play09:04

panjang

play09:14

kemudian lebar

play09:18

dollar l nah ini ya dia menampilkan

play09:21

panjang dan lebar misalkan kemudian saya

play09:25

punya kelas balok ya kemudian di sini

play09:28

dia merupakan

play09:30

Child ya dari persegi panjang

play09:34

ini saya kasih Open ya

play09:38

kemudian di sini Saya punya konstruktor

play09:41

saya punya variabel tinggi ya

play09:43

[Musik]

play09:48

kemudian saya punya konstruktor

play09:52

panjang

play09:58

integer

play10:00

lebar

play10:03

integer dan tinggi ya

play10:04

[Musik]

play10:08

Ia merupakan super

play10:11

dari panjang koma lebar ya

play10:16

kemudian saya set t = tinggi

play10:20

tinggi

play10:22

nah pada kelas persegi panjang saya

play10:24

punya method yang sama dengan kelas r

play10:27

nya misalkan display

play10:37

ini misalkan dia menampilkan panjang ya

play10:45

ini saya samain aja ya Ini apa aja

play10:49

saya copas

play10:52

saya tambahkan tinggi

play10:59

Nah di sini ada tanda merah nah ini

play11:02

artinya kita harus menambahkan overade

play11:04

di sini

play11:05

tinggal

play11:07

kemudian di kelas itu jangan lupa kita

play11:10

tambahkan Open di sini

play11:12

ya

play11:19

Open makanya dia agak error ya karena

play11:22

sudah saya tambahkan akses ininya open

play11:27

nanti kita akan belajar tentang akses

play11:29

modifier ya

play11:33

ada private ada protect dan sebagai

play11:37

nya

play11:39

nanti kita akan belajar lebih detail apa

play11:43

sih beda protect kemudian Open dan

play11:45

sebagainya

play11:53

nah ini methodnya sama nih sama-sama

play11:55

display antara dengan si balok nah

play11:58

Ketika saya klik objeknya misalkan Pak

play12:01

Ardi ya kalau nih

play12:07

ininya misalkan 1 5 7 misalkan Ketika

play12:12

saya panggil method display display

play12:17

maka yang dipanggil adalah

play12:20

pada kelas balok ini kelas SI turunannya

play12:24

eh ya ini ya display yang ini Tampilkan

play12:28

ya

play12:31

Nah kalau misalkan mau

play12:33

ini yang ini maka dia akan manggil ke

play12:37

kelas iparnya

play12:39

atau misalkan ya yang saya rasa ini

play12:42

misalkan

play12:43

kelas si A misalkan daerah a =

play12:47

persegi panjang

play12:51

a titik display nah display yang ini dia

play12:56

akan memanggil yang ini

play12:59

panjang lebar ini oh belum saya masukkan

play13:01

ya nilainya ya

play13:06

ini display masukkannya

play13:10

Oh ininya belum saya tambahkan ya satu

play13:12

sama dua

play13:14

maka yang dihasilkan adalah display pada

play13:16

klasifikasi

play13:19

nah ini saya ini display yang ini

play13:23

panjang lebar nah beda kalau misalkan

play13:25

kita balok ya balok

play13:28

kalau balok

play13:34

[Musik]

play13:38

dia akan memanggil method

play13:41

punya dia sendiri yang ini

play13:44

panjang lebar tinggi

play13:48

Nah itu

play13:50

nah ini ya seperti ini

play13:55

bisa lagi ya misalkan Saya punya kelas

play14:00

8 ini misalkan pada kelas parent ya

play14:08

hello

play14:12

hello

play14:15

Nah ini bisa kita panggil apa namanya

play14:18

itu kelas si anaknya itu memanggil sih

play14:20

method Ya methodnya itu bisa

play14:30

Hello itu bisa nah namun kalau misalkan

play14:33

nama methodnya sama yang bisa dipanggil

play14:36

adalah

play14:37

method pada kelas anak tersebut

play14:41

contohnya Ini ya display

play14:44

sama dia yang dipanggil Yang ini

play14:49

itulah pengertian override dan

play14:51

overheading ya

play14:55

Silakan dicoba ya

play14:58

coba

play15:03

nah adanya display dan display 1 Nah

play15:06

kalau display 1 ya berada pada kelas nah

play15:09

kalau yang ini berarti dia manggil pada

play15:11

kelas punyanya dia sendiri nah Silahkan

play15:15

dicoba dan dipraktekkan antara

play15:17

overloading dan override saya ulang Saya

play15:20

ulangi lagi ya kalau over loading itu

play15:23

mempunyai nama yang sama pada kelas yang

play15:25

sama namun yang membedakannya adalah

play15:27

parameter parameter masukannya

play15:31

Nah kalau misalkan jumlahnya berbeda

play15:33

misalkan ya itu Nggak masalah atau

play15:35

misalkan tipe datanya walaupun jumlahnya

play15:38

sama nih jumlah masukannya sama atau

play15:40

namun tipe datanya berbeda itu nggak

play15:43

masalah kalau kalau ini ya kalau over

play15:45

overload

play15:51

tapi kalau dia sama yang jadi masalah

play15:53

Nah kalau override dia akan memanggil

play15:56

pada kelas SI induknya eh kelasnya

play16:01

nah kalau mau memanggil kelas induknya

play16:03

berarti kita harus mengklik

play16:07

nanti materi selanjutnya kita akan

play16:09

belajar tentang

play16:11

asses modifier itu apa sih apa sih

play16:14

protektif dan sebagainya ya Silakan

play16:16

dicoba dan dipraktekkan overloading dan

play16:19

override

Rate This

5.0 / 5 (0 votes)

الوسوم ذات الصلة
OverloadingOverridingProgrammingMethodsJavaOOPClassInheritanceParametersCode Examples
هل تحتاج إلى تلخيص باللغة الإنجليزية؟