Anda di halaman 1dari 11

Nama : SULTHAN ABDULLAH N

NIM : D42116523

DICODING
1. Inheritance
2. Overriding dan Overloading
3. Polumorphism
PPT
1. Buat class MatematikaCanggih yang merupakan inherit dari class Matematika Tambahkan
method modulus[int a, intb] yang menghitung modulus dari a dan b Operator modulus adalah %.
2. Buat class MatematikaCanggihBanget yang merupakan inherit dari class MatematikaCanggih
Tambahkan method pertambahanTiga[int a, int b, int c] yang menghitung pertambahan dari a, b,
dan c.
E-BOOK THOMAS WU
Quick Check 1
1. Define The reptile class as a subclass of the Pet class. The speak method return an empty string

2. Which one of the following statements is valid?


Pet p = new Cat();
Cat c = new Pet();
 Pernyataan di atas yang valid adalah pernyataan pertama Pet p = new Cat(); karena aturan
dalam pembuatan object adalah superclass baru di lanjutkan oleh subclass.
3. Is the following code valid?
Pet p = new Dog();
System.out.println(p.fetch( ));
 Code tersebut tidak valid. Karena method fetch ini hanya terdapat pada class dog.

Quick Check 2
1. Which is the subclass and which is the superclass in this declaration?
class X extends Y { ... }
 Y adalah superclass dan X adalah subclass
2. Which visibility modifier allows the data members of a superclass to be accessible to the instances
of subclasses?
 Ada 4 jenis modifier yaitu private, default, protected dan public. Modifier yang membolehkan
subclass mengakses superclass adalah protected dan public.

Quick Check 3
1. Suppose Truck and Motorcycle are subclasses of Vehicle. Which of these declarations are invalid?
Truck t = new Vehicle();
Vehicle v = new Truck();
Motorcycle m1 = new Vehicle();
Motorcycle m2 = new Truck();
 Cuman pernyataan kedua yang valid (Vihicle v = new Truck();). Tiga lainnya tidak valid karena
tidak sesuai aturan superclass s = new subclass().
2. What is the purpose of the instanceof operator?
 Tujuan dari instanceof operator adalah untuk menguji apakah suatu object merupakan instance
tipe spesifik dari class, subclass atau interface. Output dari fungsi ini adalah Boolean (true or
false).

Quick Check 4
1. If X is a private member of the Super class, is X accessible from a subclass of Super?
 Tidak bisa. Karena modifier private hanya bisa di akses oleh class itu sendiri.
2. If X is a protected member of the Super class, is X of one instance accessible from another instance
of Super? What about from the instances of a subclass of Super?
 Tentu bisa. Karena modifier protectd ini dapat di akses oleh subclass.

Quick Check 5
1. How do you call the superclass’s constructor from its subclass?
 Dengan cara mengetik super() atau super(parameter)
2. What statement will be added to a constructor of a subclass if it is not included in the constructor
explicitly by the programmer?
 Menambahkan pernayataan super() atau super(parameter).
3. Modify the definition of GraduateStudent and UndergraduateStudent in Section 13.1 so we can
create their instances in this way:
student1 = new UndergraduateStudent();
student2 = new UndergraduateStudent("Mr. Espresso");
student3 = new GraduateStudent();
student4 = new GraduateStudent("Ms. Latte");
 tambahkan method super() pada kedua class tersebut
Quick Check 6
1. Can you create an instance of an abstract class?
 Tidak, karena kelas abstact tidak bisa instance
2. Must an abstract class include an abstract method?
 Tidak, yang penting jika memang tidak menggunakan method abstract maka kita harus
menggunakan syarat lain. Syarat class abstract ada dua yaitu sebuah class dikatakan class
abstract bila berisi method abstract atau tidak menyediakan implementasi method abstract
yang diwarisi.
3. What is wrong with the following declaration?
class Vehicle {
abstract public getVIN();
...
}
 Baris yang kedua. Karena class abstract tidak boleh berada didalam class yang tidak abstract.

Anda mungkin juga menyukai