Overloadingmethod
Upcoming SlideShare
Loading in...5
×
 

 

Statistics

Views

Total Views
273
Views on SlideShare
273
Embed Views
0

Actions

Likes
0
Downloads
6
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • static binding

Overloadingmethod Overloadingmethod Presentation Transcript

  • METHOD OVERLOADING Anjali g anjalig2009@gmail.com www.facebook.com/AnjaliG eetha twitter.com/AnjaliGeetha in.linkedin.com/in/Anjali G 9497879952
  • • If a class have multiple methods by same name but different parameters, it’s known as method overloading • It’s an example for polymorphism in java • Method overloading increases the readability of a program • There are two ways to overload a method in java 1)By changing number of arguments 2)By changing the data type • Eg :System.out.println() * In java method overloading is not possible by changing the return type of the method
  • Properties of method overloading in Java 1) Overloaded methods are bonded using static binding in Java 2) Overloaded methods are fast because they are bonded during compile time and no check or binding is required during runtime. 3)Most important rule of method overloading in Java is that two overloaded method must have different signature. method signature means in Java: 1) Number of argument to a method is part of method signature. 2) Type of argument to a method is also part of method signature 3) Order of argument also forms part of method signature provided they are of different type. 4) return type of method is not part of method signature in Java.
  • Method overloading by changing the number of arguments Class calculation { Void sum(int a, int b){ system.out.println(a+b); } Void sum(int a,int b,int c){ system.out.println(a+b+c);} Public static void main(string args[]) { Calculation obj=new calculation(); Obj.sum(10,10); Obj.sum(20,20,20) }
  • Method overloading by changing data type of argument Class calculation { Void sum(int a, int b) { system.out.println(a+b); } Void sum(double a,double b) {system.out.println(a+b);} Public static void main(string args[]) Calculation obj=new calculation(); Obj.sum(10,10); Obj.sum(20.5,20.5) }
  • Why method overloading not possible by changing the return type? • There may occur ambiguity Class calculation { int sum(int a, int b){system.out.println(a+b);} double sum(int a,int b){system.out.println(a+b);} Public static void main(string args[]) Calculation obj=new calculation(); Int result=Obj.sum(10,10);// compile error }
  • Can we overload a main method? Class simple { Public static void main(int a) { System.out.println(a); } Public static void main(string args[]) { System.out.println(“method invoked”); main(10); } }
  • Method overloading with type promotion Class calculation { Void sum(int a, long b){system.out.println(a+b);} Void sum(int a,int b,int c){system.out.println(a+b+c);} Public static void main(string args[]) Calculation obj=new calculation(); Obj.sum(10,10);// second literal promoted to long Obj.sum(20,20,20) }
  • Constructor overloading in java • Same constructor declared with different parameters in the same class is known as constructor overloading. public class Perimeter { public Perimeter() { System.out.println("From default"); } public Perimeter(int x) { System.out.println("Circle perimeter: " + 2*Math.PI*x); } public Perimeter(int x, int y) { System.out.println("Rectangle perimeter: " +2*(x+y)); } public static void main(String args[]) { Perimeter p1 = new Perimeter(); Perimeter p2 = new Perimeter(10); Perimeter p3 = new Perimeter(10, 20); } }
  • this() with Constructors • this() is used to access one constructor from another within the same class. public class Perimeter { public Perimeter(){ System.out.println("From default"); } public Perimeter(int x) { this(); System.out.println("Circle perimeter: " + 2*Math.PI*x); } public Perimeter(int x, int y) { this(100); System.out.println("Rectangle perimeter: " +2*(x+y)); } public static void main(String args[]) { Perimeter p3 = new Perimeter(10, 20); } }
  • Rules of using this() • A few restrictions exist for the usage of this(). • If included, this() statement must be the first one in the constructor. You cannot write anything before this() in the constructor. • With the above rule, there cannot be two this() statements in the same constructor (because both cannot be the first). • this() must be used with constructors only, that too to call the same class constructor (but not super class constructor).
  • Thank you..
  • If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  • Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com