×
You can use excuses to convince others, but how will you convince yourself?
--Your friends at LectureNotes
Close

Note for Java Programming - JAVA By Anup Yadav

  • Java Programming - JAVA
  • Note
  • Graphic Era University - GEU
  • Computer Science Engineering
  • 1 Topics
  • 26 Views
  • 1 Offline Downloads
  • Uploaded 9 months ago
Anup Yadav
Anup Yadav
0 User(s)
Download PDFOrder Printed Copy

Share it with your friends

Leave your Comments

Text from page-1

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

Text from page-2

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();

Text from page-3

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;

Text from page-4

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