Vous êtes sur la page 1sur 9

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("double 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 dont
play a role in overloaded resolution.
class Hello
{
}
double result;
int a;
double test(double x)
Hello h=new Hello();
int x;
{
h.test();
void test()
System.out.println("i am one double h.test(99);
{
parameter");
h.test(i);
System.out.println("i am default System.out.println(x);
h.test(50,40);
parameter");
return x*x;
h.test(32.56);
}
}
result=h.test(123.25);
void test(int a,int b)
}
System.out.println("double
{
class T8
return value:-----"+result);
System.out.println("i
am
two {
parameter");
public static void main(String as[])
}
System.out.println(a);
{
}
System.out.println(b);
int i=66;

/*
E:\java>java T8
i am default parameter
i am one double parameter
99.0
i am one double parameter
66.0
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
*/

Method overloading support polymorphism because it is one way that


Java 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. However java can automatically convert an integer into a
double. Therefore after test(int) is not found ,java elevates I to double
and then calls test(double)
In c/c++
abs
absolute value of an integer
labs
absolute value of a long integer
fabs
absolute value of a floating point
C doesnt support overloading

Overloading constructors
class Box
{
Here the Box() constructor requires three parameters .this
double length;
means that all declaration of Box objects must pass three
double width;
arguments to the Box() constructor.
double height;
Box(double l,double w,double h)
Box ob= new Box();
{
length=l; width=w; height=h;
Since Box requires three arguments, its an error to call it
}
without them.
double vol()
{ return length*width*height; }
}
height=-1;
// an
vol2=bo.vol(); //get
class T4
uninitialized Box
volume of the cube
class Box
{
}
System.out.println("vo
{
public static void
double vol()
//
lume of the cube
double length;
main(String as[])
compute
and
return
:----------="+vol2);
double width;
{
volume
}
double height;
double vol,vol1,vol2;
{
}
Box(double l,double
Box mybox=new
return
w,double h)
Box(20,30,40); //create
length*width*height;
{
boxes using the various
}
length=l;
constructors
Box(double len) //
width=w;
Box mybox1=new Box();
constructor
use
when
*/
height=h;
Box bo=new Box(6);
cube is created
E:\java>java T4
}
{
volume of the
Box()
//
vol=mybox.vol(); //get
length=width=height=len; volume of the first box
box:----------=24000.0
constructor used
}
when no dimension
System.out.println("volum volume of the
}
box1:----------=-1.0
specified
e of the box:---------volume of the cube
{
="+vol);
:----------=216.0
length=-1;
width=-1;
// use -1
vol1=mybox1.vol(); //get
to indicates
volume of the second box */
System.out.println("volum
e of the box1:---------="+vol1);

Using object as parameter


/ * using object as parameter */
class Test
public static void main(String as[])
{
{
int a,b;
Test ob1=new Test(99,88);
Test(int i,int j)
Test ob2=new Test(99,88);
{
Test ob3=new Test(99,77);
a=i;
Test ob4=new Test(99,55);
b=j;
Test ob5=new Test(-1,-1);
}
Test ob6=new Test(99,77);
boolean equals(Test o)
{
if(o.a==a && o.b==b)
return true;
else return false;
}
}
class T3
{

/*
E:\>java T3
ob1==ob2:
ob2==ob3:
ob3==ob4:
ob4==ob5:
System.out.println("ob1==ob2:\t"+ob1.equals(ob2)); ob3==ob6:
System.out.println("ob2==ob3:\t"+ob2.equals(ob3));
System.out.println("ob3==ob4:\t"+ob3.equals(ob4)); */
System.out.println("ob4==ob5:\t"+ob4.equals(ob5));
System.out.println("ob3==ob6:\t"+ob3.equals(ob6));
}
}

true
false
false
false
true

The equals method inside test compares the two object for equality and return the result.
if they contain the same values, then method return true. Otherwise it returns false. the parameter o in
equals() specifies Test as its type. One of the most common uses of object parameters involves constructors
frequently you will want to construct a new object so that it is initially the same as some existing object.

class Box
{
double length;
double width;
double height;
Box(double l,double w,double h)
{
length=l;
width=w;
height=h;
}
Box()
// constructor used
when no dimension specified
{
length=-1;
width=-1;
// use -1 to indicates
height=-1;
// an uninitialized
Box
}
double vol()
// compute and
return volume
{
return length*width*height;
}
Box(double len) // constructor
use when cube is created
{
length=width=height=len;
}
}

class T4
{
public static void main(String
as[])
{
double vol,vol1,vol2;
Box
mybox=new
Box(20,30,40); //create boxes
using the various constructors
Box mybox1=new Box();
Box bo=new Box(6);

*/
E:\java>java T4
volume of the box:---------=24000.0
volume of the box1:----------=-1.0
volume
of
the
cube
:----------=216.0
*/

vol=mybox.vol();
//get volume
of the first box
System.out.println("volume of the
box:----------="+vol);
vol1=mybox1.vol(); //get volume
of the second box
System.out.println("volume of the
box1:----------="+vol1);
vol2=bo.vol(); //get volume of
the cube
System.out.println("volume of the
cube :----------="+vol2);
}
}

PROGRAM
BY
JLC
OBJECT
AS
PARAMETER

CALL BY VALUE
//call by value
class Hello
System.out.println("before call");
{
System.out.println("a="+a);
void disp(int i,int j)
System.out.println("b="+b);
{
i*=2;
Hello h=new Hello();
j/=2;
h.disp(a,b);
} }
System.out.println("AFTER
class T7
call");
{
System.out.println("a="+a);
public static void main(String System.out.println("b="+b);
as[])
{
}
int a=99;
}
int b=77;

/*
E:\>java T7
before call
a=99
b=77
AFTER call
a=99
b=77
*/

CALL BY VALUE

When you pass a simple type to a method, it is passed by value. Thus what occurs to the parameter that
receives the argument has no effect outside the method. Here the operation occur inside disp() have no effect
on the values of a and b used in the call. Their values didn't change to 198 and 38.

CALL BY REFERENCE
//call by reference
class Hello
{
int a,b;
Hello(int i,int j)
{
a=i;
b=j;
}
void disp(Hello o) //pass an object
{
o.a*=2;
o.b/=2;
}
}

class T6
{
public static void main(String
as[])
{
Hello h=new Hello(15,20);
System.out.println("before call");
System.out.println("a="+h.a);
System.out.println("b="+h.b);
h.disp(h);
System.out.println("AFTER
call");
System.out.println("a="+h.a);
System.out.println("b="+h.b);

}
}
/*
E:\>java T6
before call
a=15
b=20
AFTER call
a=30
b=10
/*

CALL BY REFERENCE

When you pass an object to a method the situation change dramatically. Because objects are passed by
Reference. When 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. When object reference is
passed to a method, the reference itself is passed by use of call-by-value.

FINAL
A variable can be declared as final .it is a common coding convention to choose all uppercase identifiers for
final variables. Variables declared a final do not occupy memory on a per-instance basic. thus ,a final
variables is essentially a constant.
NOTE: --------- when a simple is passed to a method, it is done by use of call-by-value. Objects are passed
by use of call-by-reference.

Returning objects
A method can return any type of data, including class type that you create. Here incbyten() method returns
an object in which the value of a is ten greater than it is in the invoking object.
//returning objects
class Test
}
class T5
{
}
{
int a;
public static void main(String as[])
Test(int i)
/*
{
{
Test ob1=new Test(15);
a=i;
E:\>java T5
Test ob2;
}
ob1.a=15
ob2=ob1.incbyten();
Test incbyten()
System.out.println("ob1.a="+ob1.a); ob2.a=25
{
AFTER
SECOND
System.out.println("ob2.a="+ob2.a); ob2.a
Test t=new Test(a+10);
INCREASE:-------:
ob2=ob2.incbyten();
return t;
System.out.println("ob2.a AFTER ob2.a=35
}
SECOND INCREASE:-------:");
}
System.out.println("ob2.a="+ob2.a); */

PROGRAM BY JLC

Recursion
//factorial number
Factorial f=new Factorial();
System.out.println("FACTORIAL
{
3 IS :"+f.fact(3));
int fact(int n)
System.out.println("FACTORIAL
{
4 IS :"+f.fact(4));
int result;
System.out.println("FACTORIAL
if(n==1)return 1;
5 IS :"+f.fact(5));
result=fact(n-1)*n;
System.out.println("FACTORIAL
2 IS :"+f.fact(2));
return result;
System.out.println("FACTORIAL
}
6 IS :"+f.fact(6));
}
System.out.println("FACTORIAL
class T10
7 IS :"+f.fact(7));
{
public static void main(String System.out.println("FACTORIAL
9 IS :"+f.fact(9));
as[])
{

class Factorial

}
OF

OF /*
E:\>java T10
OF FACTORIAL OF 3 IS :6
FACTORIAL OF 4 IS :24
OF FACTORIAL OF 5 IS :120
FACTORIAL OF 2 IS :2
OF FACTORIAL OF 6 IS :720
FACTORIAL OF 7 IS :5040
OF FACTORIAL OF 9 IS :362880
OF */

Java supports recursion. Recursion is the process of defining something in term of itself. A method that calls
itself is said to be recursive. when fact() is called with an argument of 1,the function returns 1;otherwise it
returns the product of fact(n-1)*n. to evaluate this expression ,fact() is called with n-1.this process repeats
until n equals 1 and the call to the method being returning.
When a method calls itself, new local variables and parameter are allocated storage on the stack, and the
method code is executed with these new variables from the start. a recursive call does not make a new copy
of the method. Only the argument is new. The main advantage to recursive methods is that they can be used
to create clearer and simpler versions of several algorithms that can their iterative relatives. When you
writing recursive methods, you must have an if statement somewhere to force the method to return without
the recursive calls being executed. If you don't do this, once you call the method, it will never return.

Introducing access control


class Test
{
int a; //default access
public int b; //public access
private int c; //private access
void setc(int i) //set c's value
{
c=i;
}
int getc() //get c's value
{
return c;
}
}

class T11
{
public static void main(String as[])
{
Test ob=new Test();
ob.a=10;
ob.b=20;
//ob.c=77; //error
ob.setc(100);
System.out.println(ob.a);
System.out.println(ob.b);
System.out.println(ob.getc());

/*
E:\>java T11
10
20
100
*/

Program
by
Rajiv shyam

Member c is given private access. This means that it cannot accessed by code outside of its class .so , inside
the class T11 c cannot be used directly. It must be accessed through its public methods. setc() and getc().
ob.c=100 // error!
Encapsulation links data with the code that manipulates it. Encapsulation provides another important
attribute : access control.
Java support a rich set of access specifies.
Javas access specifiers are public, private, and protected. Java also defines a default access level. Protected
applies Only when inheritance is invoked.
public ------ then that member can be accessed by any other code.
private ------- then that member can only be accessed by other member of its class.
Main has always been preceded by the public specifier.

static
when you will want to define a class member that will be used independently of any object of that class.
normally a class member must be accessed only in conjunction with an object of its class. normally a class
member must be accessed only in conjunction with an object of its class. it is possible to create a member
that can be used by itself, without reference to a specific instance. to create such a member ,precede its
declaration with the keyword static. when a member is declare, it can be accessed before any objects of its
class are created, and without reference to any object.
you can declare both method and variable to be static.
main() is declared as static because it must be called before any object exist.
Instance variables declared as static are, essentially, global variables. when objects of its class are declared,
no copy of a static variable is made. Instead ,all instances of the class share the same static variable.
methods declared as static have several restrictions:--------- they can only call other static methods.
they must only access static data.
they cannot refer to this or super in any way.(the keyword super relates to inheritance)

class T12
{
static int a=66;
static int b;
static void test(int x)
{
System.out.println("X="+x);
System.out.println("a="+a);
System.out.println("b="+b);
}

static
{
System.out.println("static block
initialized");
b=a*4;
}
public static void main(String
as[])
{
test(55);

}
}

/*
E:\>java T12
static block initialized
X=55
a=66
b=264
*/
when Use Static class is loaded, all of the static statements are run. first set a=66 then the static block
executes, and finally ,b is initialized to a*4 or 264. then main is called, which calls test(),passing 77 to x. the
three println() statements refer to the two static variable a and b. if you wish to call a static method from
outside its class by

classname.method()
here classname is the name of the class in which the static method is declared.
class Stat
}
{
}
static int a=33;
class S1
static int b=44;
{
static void callme()
public static void main(String as[])
{
{
System.out.println("a="+a);
Stat.callme();
System.out.println("b="+Stat.b);

}
}
/*
E:\>java S1
a=33
b=44
*/

Array revisited
class L1
{
public static void main(String
as[])
{
int a1[]=new int[10];
int a2[]={3,5,7,9};
int a3[]={4,8,3,6,9,-10};

System.out.println("Length
a1:------------="+a1.length);
System.out.println("Length
a2:------------="+a2.length);
System.out.println("Length
a3:------------="+a3.length);
}
}

of
/*
of E:\>java L1
Length of a1:------------=10
of Length of a2:------------=4
Length of a3:------------=6
*/

NESTED AND INNER CLASSES


Define a classes within another class is called nested classes. The scope of the nested class is within the
class. If class B is defined within class A, then B is known to A, but not outside of A. A nested class has
access to the members, including private members, of the class in which it is nested. Enclosing class does
not have access to the members of the nested class.
There are two type of nested class: ------------- Static
Non-static ----- inner class
A static nested class is one which has to define with static keyword or which has static modifier. Because of
the static, it must access the members of its enclosing class through an object. it cant refer to members of its
enclosing class directly.
The most important types of the nested class is the inner class. An inner class is a non-static nested class. It
has access to all of the variables and methods of its outer class and may refer to them directly in the same

way that other non-static methods


enclosing class.
//inner class
class Outer
{
int p=99;
void test()
{
Inner inner= new Inner();
inner.display();
}
class Inner
{

of the outer class do. Thus, an inner class is fully within the scope of its
void display()
{
System.out.println("display:p="+p);
}
}
}

class T13
{
public static void main(String as[])

{
Outer outer=new Outer();
outer.test();
}
}
*/
E:\>java T13
display:p=99
*/

Here inner class name Inner defined within the Outer class. Therefore ,any code in class Inner can directly
access the variable p. An instance method named display() is defined inside Inner. the main method of T13
creates an instance of the class Outer and invokes its test() method. that method creates an instance of class
Inner and the display method is called.
the class Inner is known only within the scope of class Outer. if any code outside the class Outer java
compiler gives Error message. An inner class has access to all of the members of its enclosing class, but the
reverse is not true. member of the inner class are known only within the scope of the inner class and may
not be used by the outer class.
class Outer
System.out.println("display:x="+x);
public static void main(String as[])
{
System.out.println("y="+y);
{
int x=88;
}
Outer outer=new Outer();
void test()
}
outer.test();
{
void showy()
}
Inner inner=new Inner();
{
}
inner.display();
//System.out.println(y); //error y not /*
}
know here!
E:\>java T13
class Inner
}
display:x=88
{
}
y=99
int y=99;
class T13
*/
void display()
{
{
Here y is declared as an instance variable of Inner. Thus it is not known outside of that class and it cannot be
used by showy().
class Outer
{
int x=88;
void test()
{
for(int i=0;i<6;i++)
{
class Inner
{
int y=99;
void display()
{
System.out.println("display:x="+x);
System.out.println("y="+y);
}
}

Inner inner=new Inner();


inner.display();
}
}
}
class T14
{
public static void main(String as[])
{
Outer outer=new Outer();
outer.test();
}
}

/*
E:\>java T14
display:x=88
y=99
display:x=88
y=99
display:x=88
y=99
display:x=88
y=99
display:x=88
y=99
display:x=88
y=99
*/

Anonymous inner class: ------which are inner classes that don't have a name.