0% found this document useful (0 votes)
24 views

MOD2 Part2

Uploaded by

sueana095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

MOD2 Part2

Uploaded by

sueana095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

CHAPTER 2

Method Overloading
Method Overloading
Having more than one method with a same name is called as method overloading. To
implement this concept, the constraints are:

 the number of arguments should be different, and/or


 Type of the arguments must be different.
class Overload
{
void test() //method without any arguments
{
System.out.println("No parameters");
}
void test(int a) //method with one integer argument
{
System.out.println("Integer a: " + a);
}
void test(int a, int b) //two arguments
{
System.out.println("With two arguments : " + a + " " + b);
}
void test(double a) //one argument of double type
{
System.out.println("double a: " + a);
}
}
class OverloadDemo
{
public static void main(String args[])
{
Overload ob = new Overload();
ob.test();
ob.test(10);
ob.test(10, 20);
ob.test(123.25);
}
}
Overloading Constructors

One can have more than one constructor for a single class if the number and/or type of arguments are different. Consider the following
code:
class OverloadConstruct
{
int a, b;
OverloadConstruct()
{
System.out.println("Constructor without arguments");
}
OverloadConstruct(int x)
{
a=x;
System.out.println("Constructor with one argument:"+a);
}
OverloadConstruct(int x, int y)
{
a=x; b=y;
System.out.println("Constructor with two arguments:"+ a +"\
t"+ b);
}
}
class OverloadConstructDemo
{
public static void main(String args[])
{
OverloadConstruct ob1= new
OverloadConstruct();
OverloadConstruct ob2= new
OverloadConstruct(10);
OverloadConstruct ob3= new
OverloadConstruct(5,12);
Using Objects as Parameters
Just similar to primitive types, even object of a class can also be passed as a parameter to any method. Consider the example given below –

class Test
{
int a, b;
Test(int i, int j)
{
a = i;
b = j;
}

boolean equals(Test ob)


{
if(ob.a == this.a && ob.b == this.b)
return true; o/p:
else
return false; ob1 == ob2: true
}
} ob1 == ob3: false
class PassOb
{
public static void main(String args[])
{
Test ob1 = new Test(100, 22);
Test ob2 = new Test(100, 22);
Test ob3 = new Test(-1, -1);
System.out.println("ob1 == ob2: " + ob1.equals(ob2));
System.out.println("ob1 == ob3: " + ob1.equals(ob3));
}
}
Argument Passing
In Java, there are two ways of passing arguments to a method.
 Call by value : This approach copies the value of an argument into the
formal parameter of the method.
 Therefore, changes made to the parameter of the method have no effect on
the argument.
 Call by reference: In this approach, a reference to an argument is passed to
the parameter.
 Inside the subroutine, this reference is used to access the actual argument
specified in the call.
 This means that changes made to the parameter will affect the argument
used to call the subroutine.
 In Java, when you pass a primitive type to a method, it is passed by value.
 When you pass an object to a method, they are passed by reference
class Test
{ int a, b;
Test(int i, int j)
{
a = i; b = j;
}
void meth(Test o)
{ o/p:
o.a*= 2;
o.b/= 2; before call: 15 20
after call: 30 10
}
}
class CallByRef
{
public static void
main(String args[])
{
Test ob = new Test(15,
20);
System.out.println("before call: " + ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("after call: " + ob.a + " " + ob.b);
}
}
Returning Objects
class Test
{
int a;
Test(int i)
{
a = i;
}
Test incrByTen()
{
Test temp = new Test(a+10);
return temp; o/p:
}
ob1.a: 2
} ob2.a: 12
class RetOb ob2.a after second increase: 22
{
public static void main(String args[])
{
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob2.a after second
increase: " + ob2.a);
}
}
Recursion

A method which invokes itself either directly or indirectly is called as


recursive method. Every recursive method should satisfy following
constraints:

 It should have at least one non-recursive terminating condition.

 In every step, it should be nearer to the solution (that is, problem


size must be decreasing)
class Factorial
{
int fact(int n)
{
if (n==0)
return 1;
return n*fact(n-1); o/p:
} Factorial of 3 is 6
} Factorial of 8 is 40320

class FactDemo
{
public static void main(String args[])
{
Factorial f= new Factorial();

System.out.println("Factorial 3 is "+ f.fact(3));


System.out.println("Factorial 8 is "+ f.fact(8));
}
}
Introducing Access Control

Encapsulation feature of Java provides a safety measure viz. access


control.
Using access specifiers, we can restrict the member variables of a class
from outside manipulation.
Java provides following access specifiers:
 public
 private
 protected
 Along with above access specifiers, Java defines a default access level.
1. Private: The access level of a private modifier is only within the class. It
cannot be accessed from outside the class.
2. Default: The access level of a default modifier is only within the package. It
cannot be accessed from outside the package. If you do not specify any access
level, it will be the default.
3. Protected: The access level of a protected modifier is within the package and
outside the package through child class. If you do not make the child class, it
cannot be accessed from outside the package.
4. Public: The access level of a public modifier is everywhere. It can be accessed
from within the class, outside the class, within the package and outside the
package.
class Test
{ int a;
public int b; private int c;

void setc(int i)
{
c = i;
}

int getc()
{
return c;
}
}
class AccessTest
{
public static void main(String args[])
{
Test ob = new Test();
ob.a = 10;
ob.b = 20;
// ob.c = 100; // inclusion of this line is
Error!
ob.setc(100);
System.out.println("a, b, and c: " + ob.a + " " +
ob.b + " “ +ob.getc());
}
}
Understanding static

When a member is declared static, it can be accessed before any


objects of its class are created, and without reference to any object.
Instance variables declared as static are 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.
class UseStatic
{
static int a = 3;
static int b;

static void meth(int x) //static


method
{
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}

static //static block


{
System.out.println("Static blockinitialized."); b = a *
4;
}

public static void main(String args[]) Static block initialized.


{ x=42
meth(42); a=3
} b=12
}
Using final

The keyword final can be used in three situations in Java:


 To create the equivalent of a named constant.

 To prevent method overriding


 To prevent Inheritance
To create the equivalent of a named constant: A variable can be declared as final.
Doing so prevents its contents from being modified. This means that you must initialize a
final variable when it is declared. For example:
 final int FILE_NEW = 1;
 final int FILE_OPEN = 2;
 final int FILE_SAVE = 3;
 final int FILE_SAVEAS = 4;
 final int FILE_QUIT = 5;
Introducing Nested and Inner Classes

display: outer_x = 100

You might also like