Professional Documents
Culture Documents
MULTILEVEL INHERITENCE
BY: DE ROSAL IGN. MOSES S.
POLYMORPHISM
WHAT IS POLYMORPHISM?
// Metode
public void setData(String nama, String nim) {
this.nama = nama;
this.nim = nim;
}
public void setData(String nama) {
this.nama = nama;
}
public void cetakData() {
System.out.println("Nim siswa = " + nim);
System.out.println("Nama siswa = " + nama);
}
}
EXAMPLE METHOD OVERLOADING CONT’D
//berkas : KegiatanSiswa.java
public class KegiatanSiswa extends KelasSiswa {
// Atribut
String kegiatan;
// Metode both method
public void setKegiatan(String kegiatan) {
this.kegiatan = kegiatan; inherited
}
public void cetakKegiatan() {
System.out.println("Kegiatan siswa = " + kegiatan);
}
//Metode utama
public static void main(String[] args) {
KegiatanSiswa kSiswa1 = new KegiatanSiswa();
kSiswa1.setData("Boni M angunjiwo","A11.2013.00001");
kSiswa1.setKegiatan("DNCC");
kSiswa1.cetakData();
kSiswa1.cetakKegiatan();
KegiatanSiswa kSiswa2 = new KegiatanSiswa();
kSiswa2.setData("Rubi Arjuna");
kSiswa2.setKegiatan("Basket");
kSiswa2.cetakData();
kSiswa2.cetakKegiatan();
}
}
EXAMPLE METHOD OVERLOADING [2]
//berkas : KelasSiswa2.java
public class KelasSiswa2 {
// Atribut
String nama;
String nim;
// Metode
public void setDataSiswa(String nama, String nim) {
this.nama = nama;
this.nim = nim;
}
Not Allowed
Allowed
OVERRIDING MODIFIER CONT’D
• You will get a compile-time error if you attempt to change an instance method
in the superclass to a class method in the subclass, and vice versa.
Not Allowed
SUMMARY OVERRIDING VS HIDDING
http://docs.oracle.com/javase/tutorial/java/IandI/override.html
POLYMORPHISM WITH KEYWORD “SUPER”
• If your method overrides one of its superclass's methods,
you can invoke the overridden method through the use of
the keyword super.
• Example:
POLYMORPHISM WITH KEYWORD “SUPER” CONT’D
KEYWORD “FINAL” IN METHOD
• The final keyword in a method declaration to indicate that
the method cannot be overridden by subclasses.
• Example final keyword in method:
KEYWORD “FINAL” IN METHOD CONT’D
KEYWORD “FINAL” IN CLASS
• The final keyword in a class declaration to indicate that
the class cannot be inherit by subclasses.
• Example final keyword in class:
KEYWORD “FINAL” IN VARIABLE
• The final keyword in a variable declaration to indicate
that the variable is constant (cannot modified).
• Example final keyword in method:
KEYWORD “FINAL” IN VARIABLE CONT’D
MULTILEVEL INHERITENCE
DEFINITION OF IHERITENCE (REVIEW)
• A class that is derived from another class is called a subclass
(also a derived class, extended class, or child class).
• The class from which the subclass is derived is called a
superclass (also a base class or a parent class).
• The idea of inheritance is simple but powerful
• A subclass inherits all the members (fields, methods, and nested
classes) from its superclass. Constructors are not members, so
they are not inherited by subclasses, but the constructor of the
superclass can be invoked from the subclass.
MULTILEVEL INHERITENCE
• Every subclass only have one superclass.
• Java was designed without multiple inheritance but was
designed with multilevel inheritance. Animal
Animal Mamals
Mamals
Dog
Dog