0% found this document useful (0 votes)
6 views25 pages

Module 2

Uploaded by

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

Module 2

Uploaded by

Manasa H
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Object Oriented Programming with JAVA BCS306A

MODULE -2
Syllabus:

Introducing Classes: Class Fundamentals, Declaring Objects, Assigning Object Reference Variables,
Introducing Methods, Constructors, The this Keyword, Garbage Collection.

Methods and Classes: Overloading Methods, Objects as Parameters, Argument Passing, Returning
Objects, Recursion, Access Control, Understanding static, Introducing final, Introducing Nested and Inner
Classes.

Class is a basis of OOP languages. It is a logical construct which defines shape and nature of an object.
Entire Java is built upon classes.

3.1 Class Fundamentals


Class can be thought of as a user-defined data type. We can create variables (objects) of that data type.
So, we can say that class is a template for an object and an object is an instance of a class. Most of the
times, the terms object and instance are used interchangeably.

The General Form of a Class


A class contains data (member or instance variables) and the code (member methods) that operate on
the data. The general form can be given as –

class classname
{
type var1;
type var2;
…….
type method1(para_list)
{
//body of method1
}

type method2(para_list)
{
//body of method2
}
………..
}

Here, classname is any valid name given to the class. Variables declared within a class are called as
instance variables because every instance (or object) of a class contains its own copy of these
variables. The code is contained within methods. Methods and instance variables collectively called as
members of the class.

A Simple Class
Here we will consider a simple example for creation of class, creating objects and using members of the
class. One can store the following program in a single file called BoxDemo.java. (Or, two classes can be
saved in two different files with the names Box.java and BoxDemo.java.)

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

class Box
{
double w, h, d;
}
class BoxDemo
{
public static void main(String args[])
{
Box b1=new Box();
Box b2=new Box();
double vol;

b1.w=2;
b1.h=4;
b1.d=3;

b2.w=5;
b2.h=6;
b2.d=2;

vol=b1.w*b1.h*b1.d;
System.out.println("Volume of Box1 is " + vol);

vol=b2.w*b2.h*b2.d;
System.out.println("Volume of Box2 is " + vol);
}
}

The output would be –


Volume of Box1 is 24.0
Volume of Box1 is 60.0

When you compile above program, two class files will be created viz. Box.class and BoxDemo.class.
Since main() method is contained in BoxDemo.class, you need to execute the same.

In the above example, we have created a class Box which contains 3 instance variables w, h, d.
Box b1=new Box();
The above statement creates a physical memory for one object of Box class. Every object is an instance
of a class, and so, b1 and b2 will have their own copies of instance variables w, h and d. The memory
layout for one object allocation can be shown as –

Box Class

main()
{ Box Instance
Box b1 = new Box( );
}

BoxDemo Class

Heap
JVM

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

3.2 Declaring Objects


Creating a class means having a user-defined data type. To have a variable of this new data type, we
should create an object. Consider the following declaration:
Box b1;
This statement will not actually create any physical object, but the object name b1 can just refer to the
actual object on the heap after memory allocation as follows –
b1 = new Box ();

We can even declare an object and allocate memory using a single statement –
Box b1=new Box();
Without the usage of new, the object contains null. Once memory is allocated dynamically, the object b1
contains the address of real object created on the heap. The memory map is as shown in the following
diagram –

Statement Effect
null
Box b1;
b1

b1 = new Box();
w
b1
h
d
Closer look at new
The general form for object creation is –
obj_name = new class_name();

Here, class_name() is actually a constructor call. A constructor is a special type of member function
invoked automatically when the object gets created. The constructor usually contains the code needed
for object initialization. If we do not provide any constructor, then Java supplies a default constructor.

Java treats primitive types like byte, short, int, long, char, float, double and boolean as ordinary variables
but not as an object of any class. This is to avoid extra overhead on the heap memory and also to
increase the efficiency of the program. Java also provides the class-version of these primitive types that
can be used only if necessary. We will study those types later in detail.

With the term dynamic memory allocation, we can understand that the keyword new allocates memory for
the object during runtime. So, depending on the user’s requirement memory will be utilized. This will
avoid the problems with static memory allocation (either shortage or wastage of memory during runtime).
If there is no enough memory in the heap when we use new for memory allocation, it will throw a run-time
exception.

3.3 Assigning Object Reference Variables


When an object is assigned to another object, no separate memory will be allocated. Instead, the second
object refers to the same location as that of first object. Consider the following declaration –
Box b1= new Box();
Box b2= b1;

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

Now both b1 and b2 refer to same object on the heap. The memory representation for two objects can be
shown as –

b1 w
h
d

b2

Thus, any change made for the instance variables of one object affects the other object also. Although b1
and b2 both refer to the same object, they are not linked in any other way. For example, a subsequent
assignment to b1 will simply unhook b1 from the original object without affecting the object or affecting
b2. For example:

Box b1 = new Box();


Box b2 = b1;
// ...
b1 = null;

Here, b1 has been set to null, but b2 still points to the original object.

NOTE that when you assign one object reference variable to another object reference variable, you are
not creating a copy of the object, you are only making a copy of the reference.

3.4 Introducing Methods


A class can consist of instance variables and methods. We have seen declaration and usage of instance
variables in Program 2.1. Now, we will discuss about methods. The general form of a method is –

ret_type method_name(para_list)
{
//body of the method
return value;
}

Here, ret_type specifies the data type of the variable returned by the method. It may be any
primitive type or any other derived type including name of the same class. If the
method does not return any value, the ret_type should be specified as void.
method_name is any valid name given to the method
para_list is the list of parameters (along with their respective types) taken the method. It may
be even empty also.
body of method is a code segment written to carryout some process for which the method is
meant for.
return is a keyword used to send value to the calling method. This line will be absent if
the ret_type is void.

Adding Methods to Box class

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

Though it is possible to have classes with only instance variables as we did for Box class of Program 2.1,
it is advisable to have methods to operate on those data. Because, methods acts as interface to the
classes. This allows the class implementer to hide the specific layout of internal data structures behind
cleaner method abstractions. In addition to defining methods that provide access to data, you can also
define methods that are used internally by the class itself. Consider the following example –

class Box
{
double w, h, d;

void volume()
{
System.out.println("The volume is " + w*h*d);
}
}

class BoxDemo
{
public static void main(String args[])
{
Box b1=new Box();
Box b2=new Box();

b1.w=2;
b1.h=4;
b1.d=3;

b2.w=5;
b2.h=6;
b2.d=2;

b1.volume();
b2.volume();
}
}

The output would be –


The volume is 24.0
The volume is 60.0

In the above program, the Box objects b1 and b2 are invoking the member method volume() of the Box
class to display the volume. To attach an object name and a method name, we use dot (.) operator. Once
the program control enters the method volume(), we need not refer to object name to use the instance
variables w, h and d.

Returning a value
In the previous example, we have seen a method which does not return anything. Now we will modify the
above program so as to return the value of volume to main() method.

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

class Box
{
double w, h, d;

double volume()
{
return w*h*d;
}
}

class BoxDemo
{
public static void main(String args[])
{
Box b1=new Box();
Box b2=new Box();
double vol;

b1.w=2;
b1.h=4;
b1.d=3;

b2.w=5;
b2.h=6;
b2.d=2;

vol = b1.volume();
System.out.println("The volume is " + vol);
System.out.println("The volume is " + b2.volume());
}
}

The output would be –


The volume is 24.0
The volume is 60.0

As one can observe from above example, we need to use a variable at the left-hand side of the
assignment operator to receive the value returned by a method. On the other hand, we can directly make
a method call within print statement as shown in the last line of above program.

There are two important things to understand about returning values:


 The type of data returned by a method must be compatible with the return type specified by the
method. For example, if the return type of some method is boolean, you could not return an
integer.
 The variable receiving the value returned by a method (such as vol, in this case) must also be
compatible with the return type specified for the method.

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

Adding Methods that takes Parameters


Having parameters for methods is for providing some input information to process the task. Consider the
following version of Box class which has a method with parameters.

class Box
{
double w, h, d;

double volume()
{
return w*h*d;
}

void set(double wd, double ht, double dp)


{
w=wd;
h=ht;
d=dp;
}
}

class BoxDemo
{
public static void main(String args[])
{
Box b1=new Box();
Box b2=new Box();

b1.set(2,4,3);
b2.set(5,6,2);

System.out.println("The volume of b1 is " + b1.volume());


System.out.println("The volume of b2 is " + b2.volume());
}
}

The output would be –


The volume of b1 is 24.0
The volume of b2 is 60.0

In the above program, the Box class contains a method set() which take 3 parameters. Note that, the
variables wd, ht and dp are termed as formal parameters or just parameters for a method. The values
passed like 2, 4, 3 etc. are called as actual arguments or just arguments passed to the method.

3.5 Constructors
Constructor is a special type of member method which is invoked automatically when the object gets
created. Constructors are used for object initialization. They have same name as that of the class. Since
they are called automatically, there is no return type for them. Constructors may or may not take
parameters.

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

class Box
{
double w, h, d;

double volume()
{
return w*h*d;
}

Box() //ordinary constructor


{
w=h=d=5;
}

Box(double wd, double ht, double dp) //parameterized constructor


{
w=wd;
h=ht;
d=dp;
}
}

class BoxDemo
{
public static void main(String args[])
{
Box b1=new Box();
Box b2=new Box();
Box b3=new Box(2,4,3);

System.out.println("The volumeof b1 is " + b1.volume());


System.out.println("The volumeof b2 is " + b2.volume());
System.out.println("The volumeof b3 is " + b3.volume());
}
}

The output would be –


The volume of b1 is 125.0
The volume of b2 is 125.0
The volume of b3 is 24.0

When we create two objects b1 and b2, the constructor with no arguments will be called and the all the
instance variables w, h and d are set to 5. Hence volume of b1 and b2 will be same (that is 125 in this
example). But, when we create the object b3, the parameterized constructor will be called and hence
volume will be 24.

Few points about constructors:


 Every class is provided with a default constructor which initializes all the data members to
respective default values. (Default for numeric types is zero, for character and strings it is null and
default value for Boolean type is false.)

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

 In the statement
classname ob= new classname();
the term classname() is actually a constructor call.
 If the programmer does not provide any constructor of his own, then the above statement will call
default constructor.
 If the programmer defines any constructor, then default constructor of Java can not be used.
 So, if the programmer defines any parameterized constructor and later would like to create an
object without explicit initialization, he has to provide the default constructor by his own.
For example, the above program, if we remove ordinary constructor, the statements like
Box b1=new Box();
will generate error. To avoid the error, we should write a default constructor like –
Box(){ }
Now, all the data members will be set to their respective default values.

3.6 The this Keyword


Sometimes a method will need to refer to the object that invoked it. To allow this, Java defines the this
keyword. this can be used inside any method to refer to the current object. That is, this is always a
reference to the object which invokes the method call. For example, in the Program 2.5, the method
volume() can be written as –
double volume()
{
return this.w * this.h * this.d;
}
Here, usage of this is not mandatory as it is implicit. But, in some of the situations, it is useful as
explained in the next section.

Instance Variable Hiding


As we know, in Java, we can not have two local variables with the same name inside the same or
enclosing scopes. (Refer Program 1.7 and a NOTE after that program from Chapter 1, Page 16 & 17).
But we can have local variables, including formal parameters to methods, which overlap with the names
of the class’ instance variables. However, when a local variable has the same name as an instance
variable, the local variable hides the instance variable. That is, if we write following code snippet for a
constructor in Program 2.5, we will not get an expected output –
Box(double w, double h, double d)
{
w=w;
h=h;
d=d;
}
Here note that, formal parameter names and data member names match exactly. To avoid the problem,
we can use –
Box(double w, double h, double d)
{
this.w=w; //this.w refers to data member name and w refers to formal parameter
this.h=h;
this.d=d;
}

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

3.7 Garbage Collection


In C and C++, dynamically allocated variables/objects must be manually released using delete operator.
But, in Java, this task is done automatically and is called as garbage collection. When no references to
an object exist, that object is assumed to be no longer needed, and the memory occupied by the object
can be reclaimed. Garbage collection occurs once in a while during the execution of the program. It will
not occur simply because one or more objects exist that are no longer used. Furthermore, different Java
run-time implementations will take varying approaches to garbage collection.

3.8 The finalize() Method


Sometimes an object will need to perform some action when it is destroyed. For example, if an object is
holding some non-Java resource such as a file handle or character font, then you might want to make
sure these resources are freed before an object is destroyed. To handle such situations, Java provides a
mechanism called finalization. By using finalization, you can define specific actions that will occur when
an object is just about to be reclaimed by the garbage collector. To add a finalizer to a class, you simply
define the finalize() method. The Java run time calls that method whenever it is about to recycle an
object of that class.

The finalize( ) method has this general form:


protected void finalize( )
{
// finalization code here
}
Here, the keyword protected is a specifier that prevents access to finalize( ) by code defined outside its
class. Note that finalize( ) is only called just prior to garbage collection. It is not called when an object
goes out-of-scope. So, we can not know when finalize() method is called, or we may be sure whether it
is called or not before our program termination. Therefore, if at all our program uses some resources, we
should provide some other means for releasing them and must not depend on finalize() method.

3.9 A Stack Class


To summarize the concepts of encapsulation, class, constructor, member initialization etc, we will now
consider a program to implement stack operations.

Concept of Stack: A stack is a Last in First Out (LIFO) data structure. Following figure depicts the basic
operations on stack:

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

Inserting an element into a stack is known as push operation, whereas deleting an element from the
stack is pop operation. An attempt made to push an element into a full stack is stack overflow and an
attempt to delete from empty stack is stack underflow.

class Stack
{
int st[] = new int[5];
int top;

Stack()
{
top = -1;
}

void push(int item)


{
if(top==4)
System.out.println("Stack is full.");
else
st[++top] = item;
}

int pop()
{
if(top==-1)
{
System.out.println("Stack underflow.");
return 0;
}
else
return st[top--];
}
}

class StackDemo
{
public static void main(String args[])
{
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();

for(int i=0; i<5; i++)


mystack1.push(i);
for(int i=5; i<10; i++)
mystack2.push(i);

System.out.println("Contents of mystack1:");
for(int i=0; i<5; i++)
System.out.println(mystack1.pop());

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

System.out.println("Contents of mystack2:");
for(int i=0; i<5; i++)
System.out.println(mystack2.pop());
}
}

3.10 Overloading Methods


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.

NOTE that, only the return type of the method is not sufficient for overloading.

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);
}
}

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

3.11 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);
}
}

Output:
Constructor without arguments
Constructor with one argument: 10
Constructor with two arguments: 5 12

3.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)
{

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

a = i;
b = j;
}

boolean equals(Test ob)


{
if(ob.a == this.a && ob.b == this.b)
return true;
else
return 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));
}
}

Output:
ob1 == ob2: true
ob1 == ob3: false

Using one object to initialize the other:


Sometimes, we may need to have a replica of one object. The usage of following statements will not
serve the purpose.
Box b1=new Box(2,3,4);
Box b2=b1;

In the above case, both b1 and b2 will be referring to same object, but not two different objects. So, we
can write a constructor having a parameter of same class type to clone an object.

class Box
{
double h, w, d;

Box(double ht, double wd, double dp)


{
h=ht; w=wd; d=dp;
}
Box (Box bx) //observe this constructor
{
h=bx.h; w=bx.w; d=bx.d;
}

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

void vol()
{
System.out.println("Volume is " + h*w*d);
}
public static void main(String args[])
{
Box b1=new Box(2,3,4);
Box b2=new Box(b1); //initialize b2 using b1
b1.vol();
b2.vol();
}
}
Output:
Volume is 24
Volume is 24

3.13 A Closer Look at 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. Keep in mind that when you create a variable of a
class type, you are only creating a reference to an object. Thus, when you pass this reference to a
method, the parameter that receives it will refer to the same object as that referred to by the argument.
This effectively means that objects are passed to methods by use of call-by-reference. Changes to the
object inside the method do affect the object used as an argument.

class Test
{ int a, b;
Test(int i, int j)
{
a = i;
b = j;
}
void meth(Test o)
{
o.a *= 2;
o.b /= 2;
}
}
class CallByRef
{
public static void main(String args[])
{

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

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);
}
}

Output:
before call: 15 20
after call: 30 10

3.14 Returning Objects


In Java, a method can return an object of user defined class.
class Test
{
int a;
Test(int i)
{
a = i;
}

Test incrByTen()
{
Test temp = new Test(a+10);
return temp;
}
}

class RetOb
{
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);
}
}

Output:
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

3.15 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);
}
}

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));

}
}

Output:
Factorial of 3 is 6
Factorial of 8 is 40320

3.16 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.

Some aspects of access control are related to inheritance and package (a collection of related classes).
The protected specifier is applied only when inheritance is involved. So, we will now discuss about only
private and public.

When a member of a class is modified by the public specifier, then that member can be accessed by any
other code. When a member of a class is specified as private, then that member can only be accessed by
other members of its class. When no access specifier is used, then by default the member of a class is
public within its own package, but cannot be accessed outside of its package. Usually, you will want to

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

restrict access to the data members of a class—allowing access only through methods. Also, there will
be times when you will want to define methods that are private to a class. An access specifier precedes
the rest of a member’s type specification. For example,
public int x;
private char ch;

Consider a program given below –

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());
}
}

3.17 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.

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

If you need to do computation in order to initialize your static variables, you can declare a static block
that gets executed exactly once, when the class is first loaded.

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 block initialized.");
b = a * 4;
}

public static void main(String args[])


{
meth(42);
}
}

Output:
Static block initialized.
x = 42
a=3
b = 12

Outside of the class in which they are defined, static methods and variables can be used independently
of any object. To do so, you need only specify the name of their class followed by the dot operator. The
general form is –
classname.method();

Consider the following program:

class StaticDemo
{
static int a = 42;
static int b = 99;

static void callme()


{
System.out.println("Inside static method, a = " + a);
}
}

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

class StaticByName
{
public static void main(String args[])
{
StaticDemo.callme();
System.out.println("Inside main, b = " + StaticDemo.b);
}
}

Output:
Inside static method, a = 42
Inside main, b = 99

3.18 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;

It is a common coding convention to choose all uppercase identifiers for final variables. Variables
declared as final do not occupy memory on a per-instance basis. Thus, a final variable is essentially a
constant.

To prevent method overriding: Sometimes, we do not want a superclass method to be overridden in


the subclass. Instead, the same superclass method definition has to be used by every subclass. In such
situation, we can prefix a method with the keyword final as shown below –
class A
{
final void meth()
{
System.out.println("This is a final method.");
}
}
class B extends A
{
void meth() // ERROR! Can't override.
{
System.out.println("Illegal!");
}
}

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A
To prevent Inheritance: As we have discussed earlier, the subclass is treated as a specialized class
and superclass is most generalized class. During multi-level inheritance, the bottom most class will be
with all the features of real-time and hence it should not be inherited further. In such situations, we can
prevent a particular class from inheriting further, using the keyword final. For example –
final class A
{
// ...
}
class B extends A // ERROR! Can't subclass A
{
// ...
}
Note:
 Declaring a class as final implicitly declares all of its methods as final, too.
 It is illegal to declare a class as both abstract and final since an abstract class is incomplete by
itself and relies upon its subclasses to provide complete implementations

NESTED CLASSES
In Java, a class can have another class as its member. The class written within another class is
called the nested class, and the class that holds the inner class is called the outer class.
Java inner class is defined inside the body of another class. Java inner class can be declared
private, public, protected, or with default access whereas an outer class can have only public or default
access.
The syntax of nested class is shown below:
class Outer_Demo

class Nested_Demo

{
}
}
Types of Nested classes
There are two types of nested classes in java. They are non-static and static nested classes.
The non-static nested classes are also known as inner classes.
 Non-static nested class (inner class)
○ Member inner class
○ Method Local inner class
○ Anonymous inner class
 Static nested class

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A

Type Description
Member Inner Class A class created within class and outside method.
Anonymous Inner Class A class created for implementing interface or extending
class.
Its name is decided by the java compiler.
Method Local Inner A class created within method.
Class
Static Nested Class A static class created within class.
Nested Interface An interface created within class or interface.

INNER CLASSES (NON-STATIC NESTED CLASSES)


Inner classes can be used as the security mechanism in Java. Normally, a class cannot be related
with the access specifier private. However if a class is defined as a member of other class, then the
inner class can be made private. This class can have access to the private members of a class.
The three types of inner classes are
 Member Inner Class
 Method-local Inner Class
 Anonymous Inner Class
Member Inner Class
The Member inner class is a class written within another class. Unlike a class, an inner class
can be private and once you declare an inner class private, it cannot be accessed from an object
outside the class.
The following program is an example for member inner class.
class Outer_class { int n=20;
private class Inner_class { public void
display() {
System.out.println(“This is an inner class”);
System.out.println(“n:”+n);
}
}
void print_inner() {
Inner_class inn = new Inner_class(); inn.display();
}
}
public class Myclass {
public static void main(String args[]) {
Outer_class out= new Outer_class();
By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC
Object Oriented Programming with JAVA BCS306A
out.print_inner();
}
}
Output:
This is an inner class
Method-local Inner Class
In Java, a class can be written within a method. Like local variables of the method, the scope of the
inner class is restricted within the method. A method-local inner class can be instantiated only within the
method where the inner class is defined. The following program shows how to use a method-local inner
class. The following program is an example for Meth- od-local Inner Class
public class Outer_class { void
Method1() {
int n = 100;
class MethodInner_class { public void
display() {

System.out.println(“This is method inner class “);


System.out.println(“n:”+n);
}
}
MethodInner_class inn= new MethodInner_class();
inn.display();
}
public static void main(String args[]) {
Outer_class out = new Outer_class();
out.Method1();
}
}
Output:
This is method inner class n: 100
Anonymous Inner Class
An inner class declared without a class name is known as an anonymous inner class. The
anonymous inner classes can be created and instantiated at the same time. Generally, they are used
whenever you need to override the method of a class or an interface. The syntax of an anonymous
inner class is as follows –
abstract class Anonymous_Inner { public
abstract void Method1();

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A
}
The following program is an example for anonymous inner class.
public class Outer_class {
public static void main(String args[]) { Anonymous_Inner inn =
new Anonymous_Inner() {
public void Method1() {
System.out.println(“This is the anonymous inner class”);
}
};
inn.Method1();
}
}
Output:
This is the anonymous inner class
Static Nested Class
A static inner class is a nested class which is a static member of the outer class. It canbe accessed
without instantiating the outer class, using other static members. Just like static members, a static
nested class does not have access to the instance variables and methods of the outer class.
Instantiating a static nested class is different from instantiating an inner class. The following program
shows how to use a static nested class.
public class Outer_class { static class
inner_class{
public void Method1() { System.out.println(“This is the
nested class”);
}
}
public static void main(String args[]) { Outer_class.inner_class obj =
new Outer_class.inner_class();
obj.Method1();
}
}
Output:
This is the nested class

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC


Object Oriented Programming with JAVA BCS306A
Advantage of java inner classes:
There are basically three advantages of inner classes in java. They are as follows:
 Nested classes represent a special type of relationship that is it can access all the
members of outer class including private.
 Nested classes are used to develop more readable and maintainable code because it logically
group classes and interfaces in one place only.
 It provides code optimization. That is it requires less code to write.

By: Mr. John Peter V, Assistant Professor, Dept of ISE, CEC

You might also like