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

Unit 3

Uploaded by

rvmehtaictspce
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Unit 3

Uploaded by

rvmehtaictspce
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit – 3 More About Methods And Classes

Que. Explain Method Overloading in Java With Example.


Method Overloading means function name is same but arguments are different.
Method Overloading is use when objects are required to perform similar task but using different
input parameters.
When we call a method in an object java match up the method name to decide which one of the
definition to execute.
We can have more than one method with the same name as long as they differ either in numbers
of parameters or type of parameters or order of parameters. This is called method overloading.

Example:
class Add
{
int a,b;
void sum (int x )
{
a = b = x;
int c = a + b;
System.out.print (“sum = ” +c);
}
void sum ( int x, int y )
{
a = x;
b = y;
int c = a + b;
System.out.print (“sum = ” +c);
}
}

class MethodOverloading
{
public static void main (String [] args)
{
Add a1 = new Add ( );
a1.sum (10);
a1.sum (10,20);
}
}

Que. Explain static keyword in java with example.

There will be times when you will want to define a class member that will be used independently
of any object of that class. However, it is possible to create a member that can be used by itself,
without reference to a specific instance.

Riddhi Mehta(ICT Department) Page 37


Unit – 3 More About Methods And Classes

To create such a member, precede its declaration with the keyword 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. You can declare both methods and variables to be static.
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 following example shows a class that has a static method, some static variables, and a static
initialization block.

Example:
class UseStatic
{
static int a = 3;
static int b=10;
static void meth(int x)
{
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
public static void main(String args[])
{
meth(42);
}
}
Output:-
x = 42
a=3
b = 10

Que. Final variables with example.

The situation when we want to keep variable constant, we may declare a variable as final.
Final variable means value of variable cannot be change during execution of program.
For example,
final int a =10;

If we try to modify this value, we will get compilation error.

Riddhi Mehta(ICT Department) Page 38


Unit – 3 More About Methods And Classes

Example:
class Final
{
public static void main(String args[])
{
final double PIE = 3.14;
double r=10;

double area=PIE * r * r;
System.out.println("Area of Circle = "+area);
}
}
Output:-
Area of Circle = 314.0

Que. What is Class? Explain Nested and Inner classes with example.

Class
Class is design time entity that consists of data and member function.
Class is a user defined data types. Class is a collection of objects of similar type.

An inner class is a class that is defined within the definition of another class but outside any
member definition. Inner classes should not be marked static.
An inner class cannot be instantiated directly, i.e. one can only instantiate an inner class by first
instantiating the outer class.
The inner class has access to all the static as well as non-static public/protected/private fields of
its enclosing outer class.
Inner classes are used to provide implementation that is intimately connected with the enclosing
outer class.
It is possible to define a class within another class; such classes are known as nested classes.
The scope of a nested class is bounded by the scope of its enclosing class.
The following program illustrates how to define and use an inner class. The class named Outer
has one instance variable named outer_x, one instance method named test( ), and defines one
inner class called Inner.

Example:
// Demonstrate an inner class.
class Outer
{
int outer_x = 100;
void test()
{
Inner inner = new Inner();
inner.display();

Riddhi Mehta(ICT Department) Page 39


Unit – 3 More About Methods And Classes

}
// this is an inner class
class Inner
{
void display()
{
System.out.println("display: outer_x = " + outer_x);
}
}
}
class InnerClassDemo
{
public static void main(String args[])
{
Outer outer = new Outer();
outer.test();
}
}

Output:-
display: outer_x = 100

It is important to realize that class Inner is known only within the scope of class Outer. The Java
compiler generates an error message if any code outside of class Outer attempts to instantiate
class Inner. Generalizing, a nested class is no different than any other program element: it is
known only within its enclosing scope.
As explained, an inner class has access to all of the members of its enclosing class, but the
reverse is not true. Members of the inner class are known only within the scope of the inner class
and may not be used by the outer class.

Que. Explain scope of variables in java.

The scope of a variable is that part of the program in which the name is visible. The scope of a
variable extends from just after its declaration to the end of the innermost enclosing block
statement. The scope of a method or constructor parameter is the entire method or constructor
body. For a control variable x declared in a for statement
for (int x = ...; ...; ...) body
The scope is the entire for statement, including the header and the body.
Within the scope of a variable or parameter x, one cannot redeclare x. However, one may declare
a variable x within the scope of a field x, thus shadowing the field. Hence the scope of a field x is
the entire class, except where shadowed by a variable or parameter of the same name, and except
for initializes preceding the field's declaration.
Java variables are classified into three categories:
(1) Instance variables
(2) Class variables
(3) Local variables

Riddhi Mehta(ICT Department) Page 40


Unit – 3 More About Methods And Classes

Instance and class variables are declared inside a class. instance variables are created when the
objects are instantiated and therefore they are associated with objects. They take different values
for each object.
Class variables are global to a class and belong to the entire set of objects that class created. Only
one location is created for each class variable.
Variables declared and used inside methods are called local variables. They are called so because
they are not available for use outside the method definition. Local variables can also be declared
inside program blocks that are defined between an opening brace {and a closing brace}. These
variables are visible to the program only from the beginning of its program block to the end of
the program block. When the program control leaves a block, all the variables in the block will
cease to exist.

{ Block 1
int x = 0;

{
: Block 2
:
int n=5;
:
}

{
Block 3
:
:
int m=10;
:
}

From the above fig. each block can contain its own set of local variable declarations. We cannot
declare a variable to have the same name as one in an outer block.
In above fig., the variable x declared in Block1 is available in all the three blocks. The variable n
declared in Block2 is available only in Block2, because it goes out of the scope at the end of
Block2. Similarly, m is accessible only in Block3.

Example:-
public static void main(String[] args) {
int a, b, c;

Riddhi Mehta(ICT Department) Page 41


Unit – 3 More About Methods And Classes

int x = 1, y = 2, z = 3;
int ratio = z/x;
final double PI = 3.141592653589;
boolean found = false;
final int maxyz;
if (z > y) maxyz = z; else maxyz = y;
}

Example 4: Scope of Fields, Parameters, and Variables

This program declares five variables or fields, all called x, and shows where each one is in scope
(visible).
The variables and fields are labeled #1, ..., #5 for reference.
class Scope {
... //
void ml(int x) { // Declaration of parameter x (#1)
... // x #1 in scope
} //
... //
void m2(int v2) { //
... // x #5 in scope
} //
... //
void m3 (int v3) { //
... // x #5 in scope
int x; // Declaration of variable x (#2)
... // x #2 in scope
} //
... //
void m4 (int v4) { //
... // x #5 in scope
{ //
int x; // Declaration of variable x (#3)
... // x #3 in scope
} //
... // x #5 in scope
{ //
int x; // Declaration of variable x (#4)
... // x #4 in scope
} //
15
... // x #5 in scope
} //
... //
int x; // Declaration of field x (#5)
... // x #5 in scope

Riddhi Mehta(ICT Department) Page 42


Unit – 3 More About Methods And Classes

Que. What is command Line argument? Write a program to sort the numbers
in ascending order, entered by using the command line argument.

Sometimes you will want to pass information into a program when you run it. This is
accomplished by passing command-line arguments to main( ).
A command-line argument is the information that directly follows the program’s name on the
command line when it is executed.

class NoCmd
{
public static void main(String args[])
{
int i,j,count=0,temp=0;
count=args.length;
int a[]=new int[count];
System.out.println("Total Number of Argument = "+count);

for(i=0;i<count;i++)
a[i]=Integer.parseInt(args[i]);

for(i=0;i<count;i++)
{
for(j=i+1;j<count;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}

for(i=0;i<count;i++)
System.out.print(a[i]);
}
}

Compile and run the above program with the command line as follows:

C:\Program Files\Java\jdk1.6.0\bin>javac Final.java


C:\Program Files\Java\jdk1.6.0\bin>java Final 10 45 87 2 6 3 8 56 41 23
Total Number of Argument = 10
2 3 6 8 10 23 41 45 56 87

Riddhi Mehta(ICT Department) Page 43


Unit – 3 More About Methods And Classes

Riddhi Mehta(ICT Department) Page 44

You might also like