70 likes | 411 Views
Polymorphism. Bayu Priyambadha, S.Kom. Polymorphism. Poly = Many, Morph = form P olymorphism refers to a principle in biology in which an organism or species can have many different forms or stages
E N D
Polymorphism Bayu Priyambadha, S.Kom
Polymorphism • Poly = Many, Morph = form • Polymorphismrefers to a principle in biology in which an organism or species can have many different forms or stages • Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon • Overloading, overriding and dynamic method binding are three types of polymorphism.
Overloading • Definetwo or more methods within the same class that share the same name, as long as their parameter declarations are different class Overloading {void test() {System.out.println("No parameters");}void test(int a, int b) {System.out.println("a and b: " + a + " " + b);}void test(float a) {System.out.println("Inside test(double) a: " + a);}}
Overriding • Abilityto define a behavior that's specific to the sub class type class Animal { public void move() { System.out.println("Animals can move"); } } class Dog extends Animal { public void move() { System.out.println("Dogs can walk and run"); } }
Dynamic Binding • has definition A mechanism by which, when the compiler can't determine which method implementation to use in advance, the runtime system (JVM) selects the appropriate method at runtime, based on the class of the object • has definition The process of binding a call to a particular method. This is performed dynamically at run-time due to the presence of polymorphism
Static Binding • applies todata valuesand static members • has definition A mechanism by which the compiler determines which method implementation to use in advance, based on the type of the reference (regardless of the actual class of the object)