Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Thursday 13 July 2017

Java Inner Class

 

Inner Class In Java

Java Inner Class

Java inner class is a class which is defined in another class(outer class) is known as inner class or nested class in java.
 

Syntax of Inner class or nested class :
 

class OuterClass_Name
{
-----
-----
-----
class InnerClass_Name
{
-----
-----
-----
}
} 

Java inner classes or nested classes can access all the data members and methods of its outer class including private data member and private methods.

There are a great relationship between java inner class and its outer class's instance.

Inner class is a part of nested class, non-static classes is known as inner class in java. 

 

Types of Nested Class

There are two types of nested class, non-static nested class and static nested class. The non-static nested class is known as inner class in java.

(1) non-static nested class(inner class)
  • member inner class
  • anonymous inner class
  • local inner class

(2) static nested class

  • static nested class

Now we will cover all non-static nested class i.e inner class and static nested class in detail. So let's starts

 

Non-Static Nested Class(inner class)


1) Member Inner Class
 

In java member inner class is a class which is defined in class but outside of a method is called member inner class.

 

Java Member Inner Class Example

Java member inner class defined in a class but outside of method of that class.

In this example we will create outer class(MemberOuter) and inner class(MemberInner) and declare a private data member in outer class and access this private data member in inner class because inner class can access all the data members and methods in inner class, including private data member and methods.


class MemberOuter

{
private int salary = 4000;//private data member of outer class
class MemberInner
{
void get()
{
System.out.println("salary is "+salary);//access in inner class
}
}
public static void main(String args[])
{
MemberOuter mo = new MemberOuter();
MemberOuter.MemberInner mi = mo.new MemberInner();
mi.get();
}
} 
output : salary is 4000 

Note : (1)  In case of java outer and inner class, Here the java compiler creates two .class files the first is MemberOuter.class and MemberOuter$MemberInner.class.

(2) If you want to instantiate inner class, you must have to create the instance of outer class and instance of inner class is created inside the instance of outer class.

2) Anonymous Inner Class

In java anonymous inner class is a class that have no name is called anonymous inner class. This class can be instantiated only once. It is usually declared inside a method or block.

Anonymous inner class can be created by two ways in java 
  • Using class(abstract or concrete)
  • Using interface

 


Java Anonymous Inner Class Example

 abstract class AIExample
{
abstract void show();
}
class Test
{
public static void main(String args[])
{
AIExample a = new AIExample()
{
void show()
{
System.out.println("Method of anonymous class");
}
};
a.show();
}
}


output : Method of anonymous class
 

The above example is performing anonymous by using abstract class.

 

Java Anonymous Inner Class Example - Using Interface

This is simple example of anonymous inner class using interface.
 

interface AnonymousExmple
{
void show();
}
 

class Test
{
public static void main(String args[])
{
AnonymousExmple ae = new AnonymousExmple()
{
public void show()
{
System.out.println("hello java ");
}
};
ae.show();
}
}


output : hello java
 

3) Java Local Inner Class 

In java local inner class is a class which is created inside a method is called local inner class in java. If you want to invokes the method of local inner class, you must instantiate this class inside the method.

Java Local Inner Class Example

class LICExample
{
private int a = 40;
void show()
{
class LICExample1
{
void get()
{
System.out.println("value is "+a);
}
}
LICExample1 l = new LICExample1();
l.get();
}
public static void main(String args[])
{
LICExample ll = new LICExample();
ll.show();
}
}

output : value is 40
 
 

Static Nested Class

1) Static Nested ClassIn java static nested class is a class which is defined in a class with static keyword.

static nested class cannot access non-static data member and methods of outer class. It can access only static data member of outer class including private.

 

Java Static Nested Class Example

class Simple
{
static int a = 700;
static class SimpleInner
{
void show()
{
System.out.println("value is"+a);
}
}
public static void main(String args[])
{
Simple.SimpleInner s = new Simple.SimpleInner();
s.show();
}
}

                                        
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate