Polymorphism come from the two Greek words ‘poly’ meaning many and ‘morphs” meaning forms. The ability to exist in different form is called polymorphism. The same variable or method can perform different tasks; the programmer has the advantage of writing flexible code.
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
Polymorphism in java, method overloading and method overriding
1. Polymorphism in Java
Method Overloading and Method Overriding
Polymorphism in Java
Method Overloading and Method Overriding
Polymorphism come from the two Greek words ‘poly’ meaning many and
‘morphs” meaning forms. The ability to exist in different form is called
polymorphism. The same variable or method can perform different tasks;
the programmer has the advantage of writing flexible code.
2. Polymorphism in Java
Method Overloading and Method Overriding
Types of Polymorphism
1. Static polymorphism (compile time polymorphism)
2. Dynamic polymorphism (Run time polymorphism)
1. Static polymorphism (compile time polymorphism)
If a method call is resolved at compile time that it is called static binding or
earlier binding or static polymorphism or compile time polymorphism.
Example-
Method overloading and method overriding by using static method, private
method and final method are example for static polymorphism.
3. Polymorphism in Java
Method Overloading and Method Overriding
2. Dynamic Polymorphism (Run Time Polymorphism)
If a method call is resolved at the time of execution it is called dynamic
binding or late binding or run time polymorphism.
Example-
Method overloading and method overriding by using instance method are
example for dynamic binding.
Binding
Resolving a method call that is identifying a definition to be executed is called
binding.
In java method call resolve according to the following rules-
4. Polymorphism in Java
Method Overloading and Method Overriding
S.R. Compile Time Polymorphism Run Time Polymorphism
1. Static method call are static binded. Instance method call is dynamic binded.
2. Private non static method call are static
binded.
3. Constructor call is statically binded.
4. Final method call is statically binded.
5. Call to non static, non private method
is statically binded when calling using
super keyword.
In JVM assembly there are 4 instructions which are used by JRE for
invoking method-
5. Polymorphism in Java
Method Overloading and Method Overriding
Polymorphism in Java
Method Overloading and Method Overriding
S.R JVM assembly Instruction Purpose Type of binding
1. Invokestatic Is used to invoke
static method
Static
2. Invokespecial Is used to invoke-
1- constructor
2- private non static
methods
3- non private, non
static method using
super
4- final method
Static
3. Invokevirtual Is used to invoke
Instance method
( non private, non
static without super)
Dynamic
4. invokeinterface Is used to invoke
interface method
Dynamic
6. Note:-
To see assembly instruction in class file:-
Syntax:-
javap -c classFileName
Example:-
javap -c Calculate
Method overloading
Writing two or more method in the same class in such a way that each
method has same name but with different method signature ( by varying
there number of argument or varying the type of argument or varying the
order of the argument) is called method overloading.
Method overloading is one of the means of implementing polymorphism.
7. Example-
public classCalculate
{
void sum(int a, int b)
{
System.out.println("Sum of two number="+(a+b));
}
void sum(inta,int b, int c)
{
System.out.println("Sum of ThreeNumber="+(a+b+c));
}
public static void main(String args[])
{
Calculatex=new Calculate();
x.sum(2,3);
x.sum(2,3,4);
}
}
Output:
Sum of two number=5
Sum of ThreeNumber=9
Note- Herepolymorphism isimplemented in sameclass.
8. Method Overriding
Writing two or more method in super and sub class such that the method have
same name and same signature is called method overriding. Method overriding
is one of the means of implementing polymorphism.
Example-
9. Programmethodoverriding
classCalculate
{
void sum(int a, int b)
{
System.out.println("Sum of two number="+(a+b));
}
}
classUseextendsCalculate
{
void sum(int a, int b)
{
System.out.println("Sum of Two Number (overloaded method)="+(a+b));
}
public static void main(String args[])
{
Usex=new Use();
x.sum(2,3);
}
}
Output:
Sum of Two Number (overloaded method)=5
Note- Herepolymorphism isimplemented in super and sub class.
10. WWW.JAVATPORTAL.COM
India's No. one Tutorials and a Solution of all Technology
Address:-
Sector 58 Noida, Uttar Pradesh 201301, India.
Mobile: +91 9458548058, 8860449650
Email: sales.javatportal@gmail.com
Website: http://www.javatportal.com