13
Method overloading In java it is possible to define two or more methods within the same class that can share the same name but different parameter declaration .in this case we can say method is overloaded, and the process is called as method overloading. Method over loading is one of the ways that implements polymorphism. class Hello { int a; int x; void test() { System.out.println("i am default parameter"); } void test(int a) { System.out.println("i am one parameter"); System.out.println(a); } void test(int a,int b) { System.out.println("i am two parameter"); System.out.println(a); System.out.println(b); } double test(double x) { System.out.println("i am one double parameter"); System.out.println(x); return x*x; } } class T9 { public static void main(String as[]) { double result; Hello h=new Hello(); h.test(); h.test(99); h.test(50,40); h.test(32.56); result=h.test(123.25); System.out.println("dou ble return value:-----"+result); } } Output of program :------- /* E:\java>java T9 i am default parameter i am one parameter 99 i am two parameter 50 40 i am one double parameter 32.56 i am one double parameter 123.25 double return value:----- 15190.5625 */ test () is overloaded four times. the first version takes no parameter the second takes one integer parameter and the third takes two integer parameters and the fourth takes one double parameter . Return types don’t play a role in overloaded resolution.

02 Overloading

  • Upload
    phani

  • View
    12

  • Download
    6

Embed Size (px)

DESCRIPTION

gjjbj

Citation preview

Method overloadingIn java it is possible to define two or more methods within the same class that can share the same name butdifferent parameter declaration .in this case we can say method is overloaded, and the process is called asmethod overloading.Method over loading is one of the ways that implements polymorphism. class Hello{int a;int x;void test({!ystem.out.println("i am default parameter";#void test(int a{!ystem.out.println("i am one parameter";!ystem.out.println(a;#void test(int a,int b{!ystem.out.println("i am two parameter";!ystem.out.println(a;!ystem.out.println(b;#double test(double x{!ystem.out.println("i am one doubleparameter";!ystem.out.println(x;return x$x;##class T9{public static void main(!tring as%&{double result;'ello h(new 'ello(;h.test(;h.test());h.test(*+,,+;h.test(-..*/;result(h.test(0.-..*;!ystem.out.println("double return value122222"3result;}}Output of program :-------4$516java7java 8)i am default parameteri am one parameter))i am two parameter*+,+i am one doubleparameter-..*/i am one doubleparameter0.-..*double return value1222220*0)+.*/.*$4test ( is overloaded four times. the first version ta9es no parameter the second ta9es one integer parameterand the third ta9es two integer parameters and the fourth ta9es one double parameter . :eturn types don;tplay a role in overloaded resolution.class Hello{int a;int x;void test({!ystem.out.println("i am defaultparameter";#void test(int a,int b{!ystem.out.println("i am twoparameter";!ystem.out.println(a;!ystem.out.println(b;#double test(double x{!ystem.out.println("i amone doubleparameter";!ystem.out.println(x;return x$x;##class T8{public static void main(!tring as%&{int i(//;double result;'ello h(new 'ello(;h.test(;h.test());h.test(i;h.test(*+,,+;h.test(-..*/;result(h.test(0.-..*;!ystem.out.println("doublereturn value122222"3result;##4$516java7java 8ox{double length;double width;double height;>ox(double l,double w,double h{length(l; width(w; height(h;#double vol({ return length$width$height; ##class Box{double length;double width;double height;>ox(double l,double w,double h{length(l;width(w;height(h;#>ox(// constructor used hen no dimension specified{length(20;width(20! // use -" to indicatesheight(20;// an uninitiali#ed Box#double vol( // compute and return volume {return length$width$height;#>ox(double len// constructor use hen cu$e is created{ length(width(height(len; ##class T%{public static void main(!tring as%&{double vol,vol0,vol.;>ox mybox(new >ox(.+,-+,,+; //create $oxes using the various constructors>ox mybox0(new >ox(;>ox bo(new >ox(/;vol(mybox.vol(; //get volume of the first $ox!ystem.out.println("volume of the box12222222222("3vol;vol0(mybox0.vol(; //get volume of the second $ox!ystem.out.println("volume of the box012222222222("3vol0;vol.(bo.vol&'! //get volume of the cu$e!ystem.out.println("volume of the cube 12222222222("3vol.;##(/):*+ava,+ava T%volume of the $ox:-----------.%///0/volume of the $ox":------------"0/volume of the cu$e :-----------."10/(/Method overloading support polymorphism because it is one way that?ava implements the @one interface, multiple methods paradigm.In this example does not define test(int therefore, when test is called with an integer argument inside overload, no matching method is found. 'owever java can automatically convert an integer into a double. 8herefore after test(int is not found ,java elevates I to doubleand then calls test(doubleIn c4c33 abs absolute value of an integer labsabsolute value ofa long integerfabsabsolute value ofa floating pointA doesn;t support overloading'ere the >ox( constructor reBuires three parameters .thismeans that all declaration of >ox objects must pass threearguments to the >ox( constructor.>ox ob( new >ox(;!ince >ox reBuires three arguments, it;s an error to call itwithout them.2sing o$+ect as parameter/ * using object as parameter */class Test{int a,b;8est(int i,int j{a(i;b(j;#boolean eBuals(8est o{if(o.a((a CC o.b((b return true;else return false;##class 8-{public static void main(!tring as%&{8est ob0(new 8est()),ox(double len 44 constructoruse when cube is created{ length(width(height(len; ##class T%{public static void main(!tringas%&{double vol,vol0,vol.;>ox mybox(new>ox(.+,-+,,+; 44create boxesusing the various constructors>ox mybox0(new >ox(;>ox bo(new >ox(/;vol(mybox.vol(; 44get volumeof the first box!ystem.out.println("volume of thebox12222222222("3vol;vol0(mybox0.vol(; 44get volumeof the second box!ystem.out.println("volume of thebox012222222222("3vol0;vol.(bo.vol(; 44get volumeofthe cube!ystem.out.println("volume of thecube 12222222222("3vol.;}}$4516java7java 8,volume of the box12222222222(.,+++.+volume of the box012222222222(20.+volume of the cube12222222222(.0/.+$434O546MB789:OB8):T 6;3646M)T)4AFGG >H IFGJ544call by valueclass 'ello{void disp(int i,int j{i$(.;j4(.;##class 8D{public static void main(!tringas%&{int a());int b(DD;!ystem.out.println("before call";!ystem.out.println("a("3a;!ystem.out.println("b("3b;'ello h(new 'ello(;h.disp(a,b;!ystem.out.println("FK85:call";!ystem.out.println("a("3a;!ystem.out.println("b("3b;##4$5167java 8Dbefore calla())b(DDFK85: calla())b(DD$4:699B7 H :5K5:5NA5 Lhenyoupass anobjectto a method thesituation changedramatically.>ecause objects arepassedby:eference. Lhen you creating a variable of a class type , you are only creating a reference to an object.change to the object inside the method do effect the object used as an argument. Lhen object reference ispassed to a method, the reference itself is passed by use of call2by2value.=?>69F variable can be declared as final .it is a common coding convention to choose all uppercase identifiers for final variables. Iariables declared a final do not occupy memory on a per2instance basic. thus ,a final variables is essentially a constant.>OT): --------- when a simple is passed to a method, it is done by use of call2by2value. =bjects are passedby use of call2by2reference.4eturning o$+ectsF method can return any type of data, including class type that you create. 'ere incbyten( method returnsan object in which the value of a is ten greater than it is in the invo9ing object.//returning objectsclass Test{int a;8est(int i{a(i;#8est incbyten({8est t(new 8est(a30+;return t;##class T@{public static void main(!tring as%&{8est ob0(new 8est(0*;8est ob.;ob.(ob0.incbyten(;!ystem.out.println("ob0.a("3ob0.a;!ystem.out.println("ob..a("3ob..a;ob.(ob..incbyten(;!ystem.out.println("ob..a FK85:!5A=NO INA:5F!5122222221";!ystem.out.println("ob..a("3ob..a;} }4$5167java 8*ob0.a(0*ob..a(.*ob..a FK85: !5A=NOINA:5F!5122222221ob..a(-*$4P:=Q:FM>H?GA4ecursion44factorial numberclass =actorial{int fact(int n{int result;if(n((0return 0;result(fact(n20$n;return result;##class T"/{public static void main(!tringas%&{Kactorial f(new Kactorial(;!ystem.out.println("KFA8=:IFG =K- I! 1"3f.fact(-;!ystem.out.println("KFA8=:IFG =K, I! 1"3f.fact(,;!ystem.out.println("KFA8=:IFG =K* I! 1"3f.fact(*;!ystem.out.println("KFA8=:IFG =K. I! 1"3f.fact(.;!ystem.out.println("KFA8=:IFG =K/ I! 1"3f.fact(/;!ystem.out.println("KFA8=:IFG =KD I! 1"3f.fact(D;!ystem.out.println("KFA8=:IFG =K) I! 1"3f.fact();} }4$5167java 80+KFA8=:IFG =K - I! 1/KFA8=:IFG =K , I! 1.,KFA8=:IFG =K * I! 10.+KFA8=:IFG =K . I! 1.KFA8=:IFG =K / I! 1D.+KFA8=:IFG =K D I! 1*+,+KFA8=:IFG =K ) I! 1-/.>)4 :96;;);Oefine a classes within another class is called nested classes. 8he scope of the nested class is within theclass. If class > is defined within class F, then > is 9nown to F, but not outside of F. F nested class hasaccess to the members, including private members, of the class in which it is nested. 5nclosing class doesnot have access to the members of the nested class.8here are two type of nested class1 22222222222222 ;tatic >on-static ----- inner classF static nested class is one which has to define with static 9eyword or which has static modifier. >ecause ofthe static, it must access the members of its enclosing class through an object. it can;t refer to members of itsenclosing class directly.8he most important types of the nested class is the inner class. Fn inner class is a non2static nested class. Ithas access to all of the variables and methods of its outer class and may refer to them directly in the sameway that other non2static methods of the outer class do. 8hus, an inner class is fully within the scope of itsenclosing class.//inner classclass Outer{int p());void test({Inner inner( new Inner(;inner.display(;#class Inner{void display({!ystem.out.println("display1p("3p;###class T"D{public static void main(!tring as%&{=uter outer(new =uter(;outer.test(;}}$45167java 80-display1p())$4'ere inner class name Inner defined within the =uter class. 8herefore ,any code in class Inner can directlyaccess the variable p. Fn instance method named display( is defined inside Inner. the main method of80-creates an instance of theclass =uter and invo9es its test( method. that method creates an instance of class Inner and the display method is called.theclass Inner is 9nownonly withinthescopeofclass=uter. ifany codeoutside theclass=uter javacompiler gives 5rror message. Fn inner class has access to all of the members of its enclosing class, but thereverse is nottrue. member of the inner class are 9nown only within the scope of the inner class andmaynot be used by the outer class.class Outer{int x(