How To Implement Inner Class In Java?

Published on Aug 09,2019 2.6K Views

How To Implement Inner Class In Java?

edureka.co

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

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,

Method Of 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,

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

Thus we have come to an end of this article. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this article  and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework