Polymorphism in Java, Method Overloading and Method Overriding

Views:
 
     
 

Presentation Description

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.

Comments

Presentation Transcript

Polymorphism in Java Method Overloading and Method Overriding:

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.

Polymorphism in Java Method Overloading and Method Overriding:

Polymorphism in Java Method Overloading and Method Overriding Types of Polymorphism Static polymorphism (compile time polymorphism) Dynamic polymorphism (Run time polymorphism) 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.

Polymorphism in Java Method Overloading and Method Overriding:

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-

Polymorphism in Java Method Overloading and Method Overriding:

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-

Polymorphism in Java Method Overloading and Method Overriding:

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

Slide6:

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.

Slide7:

Example- public class Calculate { 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 Three Number="+( a+b+c )); } public static void main(String args []) { Calculate x=new Calculate(); x.sum(2,3); x.sum(2,3,4); } } Output: Sum of two number=5 Sum of Three Number=9 Note- Here polymorphism is implemented in same class.

Slide8:

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-

Slide9:

Program method overriding class Calculate { void sum( int a, int b) { System.out.println ("Sum of two number="+( a+b )); } } class Use extends Calculate { void sum( int a, int b) { System.out.println ("Sum of Two Number (overloaded method)="+( a+b )); } public static void main(String args []) { Use x=new Use(); x.sum(2,3); } } Output: Sum of Two Number (overloaded method)=5 Note- Here polymorphism is implemented in super and sub class.

Slide10:

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

authorStream Live Help