3. Standart Capaian Lulusan
Menguasai konsep teoritis dan mampu
menerapkan kemampuan mendalam pada
bahasa pemrograman untuk merancang
software aplikasi dan sistem informasi yang
sedang trend pada masanya dengan
menunjukkan kinerja mandiri, bermutu, dan
terukur serta bertanggung jawab atas
pekerjaan di bidang keahliannya.
5. Polymorphism
It is a mechanism which allows to have many
forms of the method having the same name.
(Polymorphism isthe ability of an object to take
on many forms)
The most common use of polymorphism inOOP
occurs when a parent class referenceis used to
refer to a child class object.
6. Types of polym3
orphism in3/24/20
java
There are two types of polymorphism in Java:
Runtimepolymorphism(Dynamic
polymorphism)
Compiletimepolymorphism (Static
polymorphism).
7. Runtime Polymorphism
(Dynamic polymorphism)
Method overriding is a perfect example of
runtime polymorphism.
In this kind of polymorphism, reference of class Xcan
hold object of class X or an object of any sub classes
of class X.
For e.g. if class Y extends class X then both ofthe
following statements are valid:
Y obj = new Y();
//Parent class reference can be assigned to child
object
X obj = new Y();
8. Cont..,
Since in method overriding both the classes(baseclass
andchild class)havesamemethod,compile doesn’t
figure out which method to call at compile-time.
In this case JVM (Java Virtual Machine) decides
which method tocall at runtime that’s why it is
known as runtime or dynamic polymorphism.
9. Example
public class A {
public void methodA() { //Base class method System.out.println("methodA of
class A");
} }
public class B extends A {
public void methodA() { //Derived Class method System.out.println
("methodA of class B");
} }
public class Z {
public static void main (String args []) { A obj1 = new A();
// Reference and objectB
A obj2 = new B(); // A reference but B object
obj1.methodA();
obj2.methodA();
}
Output:
methodA of class A
}
Note:As you can see the methodA has different-2 forms in child and
parent class thus we can say methodA here is polymorphic.
10. Compile time Polymorphism (Static
polymorphism)
Compile time polymorphism is nothing but the
method overloading in java.
In simple terms we can say that a class can have more
than one methods with same name but with different
number of arguments or different types of arguments or
both.
11. Example
class X {
void methodA(int num) {
System.out.println ("methodA: " + num);
}
void methodA(int n1, int n2) {
System.out.println ("methodA: "+ n1 +“, "+ n2);
}
}
public class Y {
public static void main (String args []) { X Obj = new X();
double result;
Obj. methodA(20); Obj.
methodA(20, 30);
Output:
methodA: 20
}
}
Note: The class has 2 variance of methodA or we can say methodA is polymorphic
in nature since it is having 2 different forms. In such scenario, compiler is able to
figure out the method call at compile- time that’s the reason it is known as compile
time pSoalymmpaothrpKhumisamrS.,AP