--Your friends at LectureNotes

Java Programming

by Anup Yadav
Type: NoteInstitute: Graphic Era Deemed to be University Specialization: Computer Science EngineeringViews: 5Uploaded: 30 days agoAdd to Favourite

Share it with your friends

Suggested Materials

Leave your Comments


Anup Yadav
Anup Yadav
What is Polymorphism? Polymorphism gives us the ultimate flexibility in extensibility. Polymorphism is a term that describes a situation where one name may refer to different methods. In java there are two type of polymorphism: overloading type and overriding type. There are two types of polymorphism : 1. Compile-time polymorphism 2. Runtime Polymorphism In compile time Polymorphism, method to be invoked is determined at the compile time. Compile time polymorphism is supported through the method overloading concept in java. In runtime polymorphism, the method to be invoked is determined at the run time. The example of run time polymorphism is method overriding. Method OverLaoding In Java Overloading allows different methods to have same name, but different signatures where signature can differ by number of input parameters or type of input parameters or both. Overloading is related to compile time (or static) polymorphism. Method overloading means having multiple methods with same name but with different signature (number, type and order of parameters). Rule of overloading a method in Java 1) First and foremost rule to overload a method in Java is to change method signature. method signature is made of number of arguments, type of arguments
and order of arguments if they are of different types. 2) return type of method is not part of method signature, so just changing the return type will not overload method in Java. 3) You can overload static, private and final method in Java but you cannot override them. Example class OverLoad { int calculate(int a,int b) { return a+b; } double calculate(double a,double b) { return a+b; } String calculate(String a,String b) { return a+b; } } public class Demo88 { public static void main(String[] args) { int res1; double res2; String str; OverLoad ob=new OverLoad();
res1=ob.calculate(50, 60); System.out.println(res1); res2=ob.calculate(45.66, 89.77); System.out.println(res2); str=ob.calculate("Hello"," EveryBody" ); System.out.println(str); } } Ouput 110 135.43 Hello EveryBody Example2 class OverLoad { public int maximum(int a,int b) { if(a>b) return a; else return b; } public int maximum(int a,int b,int c) { int max; max=(a>b&&a>c)?a:(b>c)?b:c; return max; } public double maximum(double a,double b) { if(a>b) return a;
else return b; } public double maximum(double a,double b,double c) { double max; max=(a>b&&a>c)?a:(b>c)?b:c; return max; } } public class OverDemo { public static void main(String[] args) { OverLoad ob=new OverLoad(); System.out.println("Max= "+ob.maximum(12,34,5)); System.out.println("Max= "+ob.maximum(12.3,5.5)); System.out.println("Max= "+ob.maximum(20,67)); } } Output Max= 34 Max= 12.3 Max= 67 Method overloading and return type In Java, method cannot be overloaded if they differ only in the return type. public class OverLoad { public int funcdmo() { return 10; } public char funcdemo() { // compiler error: funcdemo() is already defined return 'a';

Lecture Notes