Java Inner Classes

Java Inner Classes

Inner Classes: 

A class within the Other class is called as Inner classes . A Outer class members or variables can be accessed by the Inner class . Inner classes can also be called as Nested classes.

Types of Inner classes or Nested classes:
  1. Member Classes
  2. Anonymous Inner Classes
  3. Local inner Classes 
  4. Static Inner Classes 
  5. Nested Interfaces
1. Member Inner Classes:

These are the normal inner classes . 

Syntax:

class <Outer class Name>
{
    variables;
    methods();
    class <Inner class Name>
    {
         Variables;
          methods();
    }
}

Object Creation for the Inner classes :

<Outer Class Name> object = new <outer class name>();

<Outer class name>.<Inner class name> InnerObject=object.new <Inner class name>();

Program using Member Inner Classes

class OuterClass

{

class InnerClass

{

   void name()

{

System.out.println("Institute name : void main Technologies");

}

          void location()
            {
                       System.out.println("location : Dilsuknagar");
            }
}
}
class MemberClassesTest 
{
public static void main(String[] args) 
{
OuterClass oc=new OuterClass();

OuterClass.InnerClass ic=oc.new InnerClass();

ic.name();

    ic.location();
}
}

Save As:

MemberClassesTest.java

Compile:

javac MemeberClassesTest.java

Run As:

java MemeberClassesTest

Output:

Institute name : void Main Technologies

location : Dilsuknagar.

2. Anonymous inner classes:

A inner class with no name and used to over-ride any abstract class or interfaces we use Anonymous inner classes. 

Syntax:

abstract class  ClassName
{
      abstract methods();
}
class MainClass
{
     statements;

     methods;
  
   ClassName cn=new className()
     {
         defining the abstract class methods and variables;
    }
}

Program using Anonymous inner classes by Abstract classes

abstract class Abstract
{
abstract void Details();
}
class AnonymousInner 
{
public static void main(String[] args) 
{
Abstract a=new Abstract()
{
void Details()
{
System.out.println("Institute Name:void Main Technologies");

System.out.println("Location:Dilsuknagar");
}
};
}
}

Save As:

AnonymousInner.java

Compile As:

javac AnonymousInner.java

Run As:

java AnonymousInner

Output:

Institute name : void Main Technologies

Location : Dilsuknagar.

Note :

if we create a class with concrete class and by using the Anonymous inner classes  then the statements belonging to the main method will only be executed and other inner class statements will not be executed .
These classes can compiled and  can be run . 

Program using Anonymous inner classes by Interfaces

interface Interface

{

void details();

}

class AnonymousInterface 

{

public static void main(String[] args) 
{
Interface a=new Interface()
{
public void details()
{
System.out.println("Institute Name:void Main Technologies");

System.out.println("Location:Dilsuknagar");
}
};
a.details();
}
}

Save As:

AnonymousInterface .java

Compile As:

javac AnonymousInterface .java

Run As:

java AnonymousInterface

Output:

 Institute name : void Main Technologies


Location : Dilsuknagar.

Note:

Here , whatever the methods declared in the interface they must be over-ridden by using public specifier before the method name since the methods in the interfaces are public by default . Hence , we have to declare as public . 

Local Inner classes :

A class which is created with in a method is called as Inner classes . These can be accessed by creating an object out of the inner class and within the method .

Syntax:

class OuterclassName
{
    variables;
     methods ()
      {
          class InnerClass
       {
             define all the methods and variables of inner class
             
       }
      
       create the object for inner class 
}
}

class Main
{
   create object for Outer class 
    
     call the method in the outer class which contains the defination of inner class.
}

Program using Local inner class

class OuterClass
{
void outerMethod()
{
class InnerClass
{
String name="Void Main Technologies";

String location="Dilsuknagar";

void innerMethod()
{
System.out.println("Institute Name:"+name);

System.out.println("Location:"+location);
}
}
InnerClass ic=new InnerClass();

ic.innerMethod();
}
}
class LocalInnerClass 
{
public static void main(String[] args) 
{
OuterClass oc=new OuterClass();

oc.outerMethod();
}
}


Save As:



LocalInnerClass.java



Compile As:


javac LocalInnerClass .java

Run As:

java LocalInnerClass

Output:

 Institute name : void Main Technologies


Location : Dilsuknagar.

Note:

Here , in local inner class complete inner class will be in the hands of outer class method . If we want to invoke the inner class then we have to create an object for outer class and then by calling the outer class method inner class gets invoked. we cant call the inner class directly using the outer class object and even cant access the inner class directly .

Static nested class:

A class with in another class can be called as nested class or inner class. It can access only the static members and static methods in the outer class .

Program using static inner class

 class StaticOuter
{
static String name="Void Main Technologies";

static class StaticInner
{
static String location="Dilsuknagar";

static void innerMethod()
{
System.out.println("Institute Name:"+StaticOuter.name);

System.out.println("Location:"+location);
}
}
}
class StaticInnerClass 
{
public static void main(String[] args) 
{
StaticOuter.StaticInner.innerMethod();
}
}

Save As:


StaticInnerClass.java



Compile As:


javac StaticInnerClass .java

Run As:

java StaticInnerClass

Output:

 Institute name : void Main Technologies


Location : Dilsuknagar.

Note:

We cant create a class as static until unless it is an inner class.  Here , we can call the static inner class methods or variables using OuterName.InnerName.MethodName(). Here we can access the data directly without creating any object.

Nested Interface:

An interface within another interface is called as nested Interface.When we want to define a method then it must be in public mode otherwise it raises a compilation error.

Syntax:

interface outer
{
    variables;
    abstract methods();

     interface inner
     {
          variables;
          abstract methods();
     }
}

Example:

interface Out
{
    int a=10;

     void display();
     
      interface In
    {
          int b=10;
       
         void show();
    }
}

Accessing :

our class must implement these interface as follows:

class <Class Name> implements <Outer Interface>.<Inner Interface>
{
    define all the abstract methods in both outer and inner interfaces()
}


Program using Nested Interface

interface Outer
{
void name();

interface Inner
{
void location();
}
}

class NestedInterface implements Outer.Inner,Outer
{
public void name()
{
System.out.println("Institute Name:"+"void main technologies");
}

public void location()
{
System.out.println("Location:"+"Dilsuknagar");
}

public static void main(String[] args) 
{
Outer o=new NestedInterface();

Outer.Inner oi=new NestedInterface();
o.name();

oi.location();
}
}

Save As:

NestedInterface .java

Compile:

javac NestedInterface .java

Run:

java NestedInterface

Output:

 Institute name : 


Location : Dilsuknagar.

Note:

In this we can access the inner interface methods or variables by creating the reference variable for <outerInterface>.<InnerInterface> . If we want only the outer Interface methods we have to access by creating a reference variable for <OuterInterface> .

If we want to access both the inner interface and outer interface methods or variables our class must extend two interfaces i.e... <OuterInterface>.<InnerInterface> and <outerInterface> .

Comments

Popular posts from this blog

Programs on Inheritance

Arrays In Java