You are on page 1of 34

POLYMORPHISM &

MULTILEVEL INHERITENCE
BY: DE ROSAL IGN. MOSES S.
POLYMORPHISM
WHAT IS POLYMORPHISM?

• The dictionary definition of polymorphism refers to a


principle in biology in which an organism or species can
have many different forms or stages.
ANALOGY IN THE BIOLOGY PRICIPLE

Light-morph or jaguar Dark-morph or melanistic jaguar


MORE ANALOGY IN THE BIOLOGY PRICIPLE
POLYMORPHISM
• This principle can also be applied to object-oriented programming
and languages like the Java language.
• Subclasses of a class can define their own unique behaviors and
but share some of the same functionality of the parent class.
• In shorthand “an object can have various shapes”.
• Implementation of the concept of polymorphism:
• Overloading: The use of one name for several method with different
parameter.
• Overriding and hidding method: Occurs when a subclass method
declaration exactly same as the method of the superclass.
POLYMORPHISM - OVERLOADING
• Overloading occurs when there are at least two methods
with the same name but have different types and number
of parameters.
• It can happen if the two methods was found in :
• the class itself or,
• both inherited or,
• one of the method is inherit dan the other found in the class itself.
• Overloading can occur in constructor or method.
EXAMPLE CONSTRUCTOR OVERLOADING
public class Bicycle{
int speed;
int gear;
//konstruktor dengan parameter speed dan gear
public Bicycle(int speed, int gear){
this.speed = speed;
this.gear = gear;
at least two
} methods same
//konstructor dengan parameter gear
public Bicycle(int gear){ found in the class
this.gear = gear;
}
//konstructor tanpa parameter
public Bicycle(){
}
//metode
void info(){
System.out.println("Speed: " + this.speed);
System.out.println("Gear: " + this.gear);
}
}
EXAMPLE CONSTRUCTOR OVERLOADING CONT’D
public class BicycleOverloading{
public static void main(String[] args){
//menggunakan konstruktor dengan parameter speed dan gear
Bicycle myBike = new Bicycle(10, 1);
myBike.info();
//menggunakan konstruktor dengan parameter gear
Bicycle yourBike = new Bicycle(1);
yourBike.info();
//menggunakan konstruktor tanpa parameter
Bicycle nnBike = new Bicycle();
nnBike.info();
}
}
EXAMPLE METHOD OVERLOADING
//berkas : KelasSiswa.java
public class KelasSiswa {
// Atribut
String nama;
String nim;

// 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;
}

public void cetakDataSiswa() {


System.out.println("Nim siswa = " + nim);
System.out.println("Nama siswa = " + nama);
}
}
EXAMPLE METHOD OVERLOADING [2] CONT’D
//berkas : KegiatanSiswa2.java
public class KegiatanSiswa2 extends KelasSiswa2 {
// Atribut
String kegiatan;
// Metode
public void setDataSiswa(String nama, String nim, String kegiatan) {
this.nama = nama;
this.nim = nim;
this.kegiatan = kegiatan;
one of the method is inherit
} dan the other found in the class
public void cetakDataKegitanSiswa () {
System.out.println("Nim siswa = " + nim);
System.out.println("Nama siswa = " + nama);
System.out.println("Kegiatan siswa = " + kegiatan);
}
//Metode utama
public static void main(String[] args) {
KegiatanSiswa2 kSiswa1 = new KegiatanSiswa2();
kSiswa1.setDataSiswa("M ilka Susanti","A11.2013.00001","Paduan Suara");
kSiswa1. cetakDataKegitanSiswa();
KegiatanSiswa2 kSiswa2 = new KegiatanSiswa2();
kSiswa2.setDataSiswa("Putri Rahayu","A11.2013.00001");
kSiswa2.cetakDataSiswa();
}
}
POLYMORPHISM - OVERRIDING
• Overriding occurs when a subclass inherits methods of the
super class, and the method is still not suitable, then sub-
class can modify the method when needed.
• The characteristic of overriding is:
For instance
• the method name,
Method
• the type of methods,
• the number of parameters, same with
• parameter names, the overridden method
• parameter types.
EXAMPLE METHOD OVERRIDING
//berkas : SuperKelas.java
public class SuperKelas {
public void metodeCetak() {
System.out.println("Di cetak di super kelas ");
}
} Method metodeCetak in
SuperKelas overridden by
//berkas : SubKelas.java
public class SubKelas extends SuperKelas { SubKelas
// override metodeCetak in Superclass
public void metodeCetak() {
System.out.println("Di cetak di sub kelas ");
}
public static void main(String[] args) {
SubKelas s = new SubKelas();
s.metodeCetak();
}
}
POLYMORPHISM - HIDDING
• If a subclass defines a class method with the same signature as a class method
in the superclass, the method in the subclass hides the one in the superclass.
• For example:
//berkas : Animal.java
For Class /
public class Animal { Static Method
//metode kelas
public static void testClassM ethod() {
System.out.println("The class" + " method in Animal.");
}
//metode instan
public void testInstanceM ethod() {
System.out.println("The instance " + " method in Animal.");
}
}
POLYMORPHISM – HIDDING CONT’D
//berkas : Cat.java
public class Cat extends Animal {
//metode kelas Cat (sub kelas) memiliki kesamaan dengan metode kelas Animal (super
// kelas) sehinga menyembunyikan metode kelas Animal (super kelas)
public static void testClassM ethod() {
System.out.println("The class method" + " in Cat.");
}
//metode instan
public void testInstanceM ethod() {
System.out.println("The instance method" + " in Cat.");
}

public static void main(String[] args) {


Cat myCat = new Cat();
Animal myAnimal = myCat;
Animal.testClassMethod(); //memaggil metode kelas pada super kelas
myAnimal.testInstanceMethod();
}
}
OVERRIDING VS HIDDING
• The distinction between hiding and overriding has
important implications.
• The version of the overridden method that gets invoked is
the one in the subclass.
• The version of the hidden method that gets invoked
depends on whether it is invoked from the superclass or
the subclass.
OVERRIDING MODIFIER
• The access specifier for an overriding method can allow more, but not less,
access than the overridden method (a protected instance method in the
superclass can be made public, but not private, in the subclass).

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

Multiple Inheritance Multilevel Inheritance


EXAMPLE MULTILEVEL INHERITANCE
EXAMPLE MULTILEVEL INHERITANCE CONT’D

At the top of the hierarchy, Object is the most general of all


classes. Classes near the bottom of the hierarchy provide
more specialized behavior.
THANKS

You might also like