Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
143 views

Lecture 3 Nested Classes

The document discusses nested classes in Java. There are two types of nested classes - non-static and static. Non-static nested classes, also called inner classes, allow classes to be declared within other classes and can access private members of the outer class. Inner classes can be used to access private members of outer classes. Method-local inner classes are declared within a method and can access local variables of that method.

Uploaded by

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

Lecture 3 Nested Classes

The document discusses nested classes in Java. There are two types of nested classes - non-static and static. Non-static nested classes, also called inner classes, allow classes to be declared within other classes and can access private members of the outer class. Inner classes can be used to access private members of outer classes. Method-local inner classes are declared within a method and can access local variables of that method.

Uploaded by

Gafeer Fable
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structures and Algorithms

Nested Classes
In Java, just like methods, variables of a class too can have
another class as its member. Writing a class within another is
allowed in Java. The class written within is called the nested
class, and the class that holds the inner class is called the outer
class.
Syntax

Following is the syntax to write a nested class. Here, the


class Outer_Demo is the outer class and the
class Inner_Demo is the nested class.

class Outer_Demo {
class Nested_Demo {
}
}
Nested classes are divided into two types

 Non-static nested classes − These are the non-static members of a class.


 Static nested classes − These are the static members of a class.

Inner Classes (Non-static Nested Classes)


Inner classes are a security mechanism in Java. We know a
class cannot be associated with the access modifier private, but if
we have the class as a member of other class, then the inner
class can be made private, and this is also used to access the
private members of a class.

Inner Class
Creating an inner class is quite simple. You just need to write a
class within a class. Unlike a class, an inner class can be private

1|P ag e
Data Structures and Algorithms

and once you declare an inner class private, it cannot be accessed


from an object outside the class.
Following is the program to create an inner class and access it.
In the given example, we make the inner class private and access
the class through a method
class Outer_Demo {
int num;

// inner class
private class Inner_Demo {
public void print() {
System.out.println("This is an inner class");
}
}

// Accessing the inner class from the method within


void display_Inner() {
Inner_Demo inner = new Inner_Demo();
inner.print();
}
}
public class TestVeh {

public static void main(String args[]) {


Outer_Demo outer = new Outer_Demo();

// Accessing the display_Inner() method.


outer.display_Inner();

}
}
Output
This is an inner class

2|P ag e
Data Structures and Algorithms

Accessing the Private Members


As mentioned earlier, inner classes are also used to access
the private members of a class. Suppose, a class is having private
members to access them. Write an inner class in it, return the
private members from a method within the inner class,
say, getValue(), and finally from another class (from which you
want to access the private members) call the getValue() method
of the inner class.
To instantiate the inner class, initially you have to instantiate
the outer class. Thereafter, using the object of the outer class,
following is the way in which you can instantiate the inner class.
Outer_Demo outer = new Outer_Demo();
Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();

The following program shows how to access the private


members of a class using inner class.
class Outer_Demo {
// private variable of the outer class
private int num = 175;

// inner class
public class Inner_Demo {
public int getNum() {
System.out.println("This is the getnum method
of the inner class");
return num;
}
}
}

public class TestVeh {

public static void main(String args[]) {


Outer_Demo outer = new Outer_Demo();

// Accessing the getNum() method.


3|P ag e
Data Structures and Algorithms

Outer_Demo outer = new Outer_Demo();


Outer_Demo.Inner_Demo inner = outer.new Inner_Demo();

System.out.println(inner.getNum());
}
}
Output
This is the getnum method of the inner class
175
Method-local Inner Class
public class Outerclass {
// instance method of the outer class
void my_Method() {
int num = 23;

// method-local inner class


class MethodInner_Demo {
public void print() {
System.out.println("This is method inner
class "+num);
}
} // end of inner class

// Accessing the inner class


MethodInner_Demo inner = new MethodInner_Demo();
inner.print();
}
}
public class TestVeh {

public static void main(String args[]) {


Outerclass outer = new Outerclass();
outer.my_Method();
}
}
Output

4|P ag e
Data Structures and Algorithms

This is method inner class 23

5|P ag e

You might also like