Java Inner and Wrapper classes
Inner class in java means a one class which is the member of another class. In Java, there are various types of inner classes. This article will help you unravel all these classes. Following pointers will be discussed in detail,
So let us get started with this Inner Class In Java article,
Inner Class in Java
1)Nested Inner Class
This class has the access to any private instance value of the outer class. Writing one class within another class is also allowed in Java. The class written within is known nested class and the class that holds the inner class is known as outer class.
Syntax
class outerplace{
class innerplace{
}
}
Example
In the example given below, We are making the inner class private and accessing the class with the help of method.
class Outer_place{
int num;
private class Inner_place{
public void print(){
System.out.println("It is an inner class");
}
}
void display_Inner(){
Inner_place inner = new Inner_place();
inner.print();
}
}
public class My_class{
public static void main(String args[]){
Outer_place outer = new Outer_place();
outer.display_Inner();
}
}
Output
Here, Outer place is the outer class and Inner place is called the inner class.
Moving on with this Inner Class In Java article,
- Accessing private Members
Inner classes are used for accessing the private members of the class too. Suppose there is a class having private members to access them. Now write an inner class within the class and access the private members from a method within the inner class.
Here is the example,
class Outer_place{
private int num = 162;
class Inner_place{
public int getNum(){
System.out.println("It is a getnum method of inner class:");
return num;
}
}
}
public class My_class{
public static void main(String args[]){
Outer_place outer = new Outer_place();
Outer_place.Inner_place inner = outer.new Inner_place();
System.out.println(inner.getNum());
}
}
Output
Moving on,
2)Method Local Inner Classes
In Java, You can write a class within the method and that will be known as local type. Like all the local variables, the scope of inner class is restricted within a method.
Example
The following example will show how a method local inner class is implemented.
public class Outerplace{
void my_Method(){
int num = 45;
class MethodInner_place{
public void print(){
System.out.println ("method for inner classes "+num);
}
}
MethodInner_place inner = new MethodInner_place();
inner.print();
}
public static void main(String args[]){
Outerplace outer = new Outerplace();
outer.my_Method();
}
}
Output
Moving on with this Inner Class In Java article,
3)Anonymous Inner class
Any inner class which is declared without the classname is called anonymous inner class. In the case of anonymous inner classes, we instantiate and declare it at the same time.
Whenever we want to override the method of class or an interface, we use this class.
Syntax
AnonymousInner obj1 = new AnonymousInner(){
public void method(){}
};
Example
abstract class AnonymousInner{
public abstract void mymethod();
}
public class Outer_class{
public static void main(String args[]){
AnonymousInner inner = new AnonymousInner(){
public void mymethod(){
System.out.println(" example of anonymous inner class");
}
};
inner.mymethod();
}
}
Output
Moving on with this Inner Class In Java article,
- As Argument Of Anonymous Inner Class
In this , If a method accepts the object of the interface, of an abstract class , or the concrete class, then we are able to implement the interface ,pass the object to the method and extend the abstract class.
Syntax
obj. method(
new class(){
public void do{}
}
);
Example
// interface
interface Message{
String greet();
}
public class My_class {
//object of interface message is accepted by this method
public void displayMessage(Message m){
System.out.println(m.greet() +
", example of anonymous inner class as argument");
}
public static void main(String args[]){
//Instantiating of class
My_class obj = new My_class();
//Passing the anonymous inner class as argument
obj.displayMessage(new Message(){
public String greet(){
return "Hey";
}
});
}
}
Output
Moving on with this Inner Class In Java article,
- Anonymous Inner Class Of A Specified Subclass
Source code
class Demo{
void show(){
System.out.println("i was in show method of class");
}
}
class Flavor1Demo{
static Demo d = new Demo(){
void show(){
super.show();
System.out.println("i was present in Flavor1Demo class");
}
};
public static void main(String[] args){
d.show();
}
}
Output
Moving on with this Inner Class In Java article,
- Anonymous Inner Class as an Implementer of Specified Interface
Source Code
class Flavor2Demo{
//class which implements Hello interface
static Hello h = new Hello(){
public void show(){
System.out.println("i was present in anonymous class");
}
};
public static void main(String[] args){
h.show();
}
}
interface Hello{
void show();
}
Output
Moving on with this Inner Class In Java article,
Static Nested Classes
These classes are not technically known as an inner classes. These classes are similar to the static member of the outer class. A static nested class do not have any access to the variables and methods of outer class. We do not need to instantiate the outer class, It can be accessed directly using the static members.
Syntax
Class outer{
Static class nested_example{}
}
Example
public class Outer{
static class Nested_Example{
public void my_method(){
System.out.println("It is the nested class");
}
}
public static void main(String args[]){
Outer.Nested_Example nested = new Outer.Nested_Example();
nested.my_method();
}
}
Output
Wrapper classes in Java
A Wrapper class is a class whose object wraps or contains primitive data types. When we create an object to a wrapper class, it contains a field and in this field, we can store primitive data types. In other words, we can wrap a primitive value into a wrapper class object.
Need of wrapper classes
- They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method (because primitive types are passed by value).
- The classes in java.util package handles only objects and hence wrapper classes help in this case also.
- Data structures in the Collection framework, such as ArrayList and Vector, store only objects (reference types) and not primitive types.
- An object is needed to support synchronization in multithreading.
- Autoboxing
The automatic conversion of primitive data types into its wrapper class objects is known as autoboxing
.
class Autoboxing {
public static void main( String args[] ) {
int a = 15; // Primitive data type
Integer I = a; // Autoboxing will occur internally.
}
}
- Unboxing
The automatic conversion of wrapper class objects into its primitive data types is known as unboxing
.
class Autoboxing {
public static void main( String args[] ) {
Integer a = new Integer(15); // Wrapper class object
int I = a;// Unboxing will occur internally.
}
}
Blog created by
SY CS-A Group No 8