Nested Classes In Java
A nested class is a class which is defined within a class or interface.
Nested class is divided in four categories:
1. Static nested class
2. Non static nested class
3. Local nested class
4. Anonymous nested class
Except static nested class all other classes are called inner class because to access all the three classes other then static class, we need to create a first object of outer class.
In case of static nested class we do not need to create an object of outer class first.
************************_*******************************
Static nested class:
A static nested class can access all the data member and static member function of its outer class.
Static nested class and non static nested class are treated as the data members of that class in which they are defined.
class Outer
{
int x = 10;
static int y = 20;
static class Inner //static nested class defined
{
void show()
{
//this statement will give error as x is non static
//because only static members are available to statuc inner class
// System.out.println(x);
System.out.println(y);
}
//defined main in inner class to make it executable
//so we can also put a main() in static nested class
public static void main(String[] args)
{
Inner i = new Inner(); //inner class object created
i.show(); //show method of static nested class invoked
}
}}
/*
the output .class file name will beOuter$Inner.class
compile as >>javac Outer.java
Run as >> java Outer$Inner
Output
20
*/
We can also access the static inner class methods outside the inner class.
Example 1: Accessing the inner class static method outside the inner class:
class Outer
{
int x = 10;
static int y =20;
static class Inner
{
static void show()
{
System.out.println(y);
}
}}
class Temp
{
public static void main(String[] args)
{
Outer.Inner.show();
//accessing the Inner class static method outside inner class
}}
/*Output (java Temp)
20
*/
Example 2: inheriting data members and methods of inner class via inheritance.
We can access the data members and methods of an inner class outside the outer class by using inheritance. This is how we can do it:
class Outer
{
int x = 10;
static int y =20;
static class Inner
{
void show()
{
System.out.println(y);
}
}}
//Outer class inherited (as inner class is treated as
//Outer class's data member so it is also inherited.)
class Temp extends Outer
{
public static void main(String[] s)
{
Inner i = new Inner();
i.show();
}}
/*Output (java Temp)
20
*/
Example 3: Inheriting the Inner static class directly
We can inherit all the inner class directly independent of Outer class. This is how we can do it:
class Outer
{
int x = 10;
static int y =20;
static class Inner
{
void show()
{
System.out.println(y);
}
}}
//inner class inherited directly by using the
//Outer class name and dot operator
class Temp extends Outer.Inner
{
public static void main(String[] args)
{
Temp i = new Temp();
i.show();
}}
/* Output
20
*/
Example 4: Inner class inheriting the outer class
Inner classes can extends the outer class also and after extending it we can access the non staticdata members also.
class Outer
{
int x = 10;
static int y =20;
static class Inner extends Outer //inner class inherited the outer class
{
void show()
{
System.out.println(x); //non static data member is now used inside a static inner class
System.out.println(y);
}
}
public static void main(String[] args)
{
Inner o = new Inner(); //object of inner class created
o.show(); //show method of inner class called
}}
/*output (java Outer)
10
20
*/
************************_*******************************
Non static Inner class
A non static nested class can access the data members and member functions of its outer class.
· To access the non static methods outside its class, we have to create Outer class object first then we can use the reference variable in which the object’s ref ID is saved to create inner class object. See the example to understand:
· Non static data member cannot have its own static data members and static member functions.
· Inheritance is same as static nested class.
For example:
class Outer
{
int x = 10; //non static data member
static int y =20; //static data member
class Inner
{
void show()
{
System.out.println(x);
System.out.println(y);
}
}
public static void main(String[] args)
{
//object of outer class created
Outer o = new Outer();
//object of inner class created by using o of Outer class
Outer.Inner i = o.new Inner();
//show method of class Inner is called from outside the inner class
i.show();
}}
/*Output
10
20
*/
If we have same data members in outer class and inner class and if we want to access the Outer class data member(Generally, the preference is always given to local variables) then we have to use this keyword. This is how we will make use of “this” keyword here to access parent data member.
Example: Accessing the Outer class data member if inner class and outer class has same data member.
class Outer
{
//data members defined
int x = 10;
int y = 20;
class Inner
{
int x = 40; //same data member in inner class
void show()
{
System.out.println(x);
System.out.println(y);
//usage of this keyword to access the parent data members
System.out.println(Outer.this.x);
}
}
public static void main(String[] args)
{
//Outer class object created
Outer o = new Outer();
//inner class object is created using outer class object
Outer.Inner i = o.new Inner();
//method show of inner class invoked
i.show();
}}
Output:
We can inherit the non static inner class by inheriting the outer class. As the non static inner class is treated as a data member of the class in which it is defined. So, it will also inherited to the class who extends the Outer class.
Example: Accessing the non static inner class data members and methods in some other class:
class Outer
{
int x = 10;
static int y =20;
class Inner
{
void show()
{
System.out.println(x);
System.out.println(y);
}
}}
//Outer class inherited (as inner class is treated as
//Outer class's data member so it is also inherited.)
class Temp extends Outer
{
public static void main(String[] s)
{
Outer o = new Outer();
Inner i = o.new Inner();
i.show();
}
}
/*Output
10
20
*/
************************_*******************************
Local Class
whenever you define a class within any block of class except the class block then it is treated as a local class. And it will treat as a local variable of that particular block in which it is defined.
Example of a local class:
class Outer
{
int x = 10;
static int y = 20;
void display()
{
//Local class defined inside a function
class Inner
{
void show()
{
System.out.println(x);
System.out.println(y);
}
}
Inner i = new Inner();
i.show();
}
public static void main(String[] args)
{
Outer p = new Outer();
p.display();
}
}
/*Output
10
20
*/
Accessing the function of a local class in outer class
We know that we cannot make object of local class outside its function. But then how we can run a local class show in outer class?
We can also run the methods of a local class in outer class. this is how we can do it:
1. We need to return the value outside a function and there is only one way to do it: we need to return the value. But what will be the return type then?
2. We will create an interface My and will implement this to inner class.
3. Then we will make the show function in inner class as public because we have to use it outside its class.
4. Then we will create inner class object and store its Reference ID in the reference variable of parent class (Interface My).
5. And finally we will create the return type of display function as My.
Example: accessing the function of a local class in outer class:
interface My //interface My defined
{
void show();
}
class Outer
{
int x = 10;
static int y = 20;
My display()
{
class Inner implements My //inner class implementing the interface
{
public void show()
{
System.out.println(x);
System.out.println(y);
}
}
My m = new Inner();
return m;
//returning the variable which holds the RID of object of the child of My Intrface
}
public static void main(String... s)
{
Outer o = new Outer();
o.display();
//accessing the local class methods in outer class
//RID is in parent's variable and we can run method of child using it.
My z = o.display();
z.show();
}}
/*output
10
20
*/
************************_*******************************
Anonymous class
We know that anonymous class in Java is a class who does not have any name. So, we can define anonymous class as inner class also.
Some rules associated with this class:
· This class can define instantiated class.
· Anonymous class cannot be used more than once.
· We cannot put any constructor in any anonymous class.
· They do not have any name.
· They must not be having any parent.
An example of inner anonymous class:
interface My //interface My defined
{
void show();
}
class Outer
{
int x = 20;
static int y = 50;
My display()
{
//local anonymous class defined inside the display function.
return (new My()
{
public void show()
{
System.out.println(x);
System.out.println(y);
}
}
);
}
//we used anonymous class here to return a value from display function.
public static void main(String[] args)
{
Outer o = new Outer();
o.display();
My z = o.display();
z.show();
}}
/*Output
20
50
*/
No comments:
Post a Comment