Skip to main content

Inner classes

Inner Classes:-class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly.
Types of Nested classes:-
There are two types of nested classes static and non-static nested classes.

(A)Non –static inner classes:-
1)    Member inner class
2)    Anonymous inner class
3)    Local inner class

(B)Static nested class

Example1:-Outer.java

class Outer
{
class Inner
{
void disp()
{
System.out.println("Hello This is Inner class Method");
}
}
public static void main(String[]args)
{
Inner in=new Inner();
in.disp();
}
}
Output:-
C:\JAVATECH>javac Outer.java
Outer.java:12: error: non-static variable this cannot be referenced from a static context
Inner in=new Inner();
         ^
1 error

Example2:-Outer.java

class Outer
{
class Inner
{
void disp()
{
System.out.println("Hello This is Inner class Method");
}
}
public static void main(String[]args)
{
Outer out=new Outer();
Outer.Inner in=out.new Inner();
in.disp();
}
}
Output:-
C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
Hello This is Inner class Method

Example3:-Outer.java

class Outer
{
private String name="Sachin";
private int roll=101;
class Inner
{
void disp()
{
System.out.println("Hello This is Inner class Method");
System.out.println(name+"  your roll number is : "+roll);
}
}
public static void main(String[]args)
{
Outer out=new Outer();
Outer.Inner in=out.new Inner();
in.disp();
}
}
Output:-
C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
Hello This is Inner class Method
Sachin  your roll number is : 101

Example4:-Outer.java

class Outer
{
private String name="Sachin";
private int roll=101;
class Inner
{
private String name="Amit";
private int roll=102;
void disp()
{
System.out.println("Hello This is Inner class Method");
System.out.println(name+"  your roll number is : "+roll);
System.out.println(Outer.this.name+"  your roll number is : "+Outer.this.roll);
}
}
public static void main(String[]args)
{
Outer out=new Outer();
Outer.Inner in=out.new Inner();
in.disp();
}
}
Output:-
C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
Hello This is Inner class Method
Amit  your roll number is : 102
Sachin  your roll number is : 101

Anonymous Inner class:-Anonymous class is class having no name, compiler decide the name of this class at compile time and it used for abstract class and interface for method implementation inside class body. In the case of Anonymous class object created by the subclass and return to the abstract class reference or to interface reference variable.

Example1:-TestM.java

abstract class Car
{
abstract void run();
}
class TestM
{
public static void main(String[]args)
{
Car v=new Car(){
public void run()
{
System.out.println("Run method implemented");
}
};
v.run();
}
}
Output:-
C:\JAVATECH>javac TestM.java
C:\JAVATECH>java TestM
Run method implemented

Example1:-TestM.java

interface Car
{
 void run();
}
class TestM
{
public static void main(String[]args)
{
Car v=new Car(){
public void run()
{
System.out.println("Run method implemented");
}
};
v.run();
}
}
Output:-
C:\JAVATECH>javac TestM.java
C:\JAVATECH>java TestM
Run method implemented


Local Inner class:-Local inner class is declared inside class inside method. Local class object must be created inside method where local class is declared.

Example1:-Outer.java
class Outer
{
private String name="Amit";
void local()
{
class Local
{
void lm()
{
System.out.println("This is local class method   "+name);
}
}
Local l=new Local();
l.lm();
}
public static void main(String[]args)
{
Outer o=new Outer();
o.local();
}
}
Output:-
 C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
This is local class method   Amit


Static Inner class:-static inner class is class which is declared inside class with static modifier and static class uses only static data of outer class only.

Example1:-Outer.java

class Outer
{
private String name="Amit";
static class Inner
{
void lm()
{
System.out.println("This is static inner class method   "+name);
}
}

public static void main(String[]args)
{
Outer.Inner ob=new Outer.Inner();
ob.lm();
}
}
Output:-
C:\JAVATECH>javac Outer.java
Outer.java:8: error: non-static variable name cannot be referenced from a static context
System.out.println("This is static inner class method   "+name);
                                                          ^1 error
Example2:-Outer.java

class Outer
{
private static String name="Amit";
static class Inner
{
void lm()
{
System.out.println("This is static inner class method   "+name);
}
}
public static void main(String[]args)
{
Outer.Inner ob=new Outer.Inner();
ob.lm();
}
}
Output:-
C:\JAVATECH>javac Outer.java
C:\JAVATECH>java Outer
This is static inner class method   Amit

Interface Nesting:-

Example1:-TestInterface.java
interface Outer
{
void get();
interface Inner
{
void show();
}
}
class TestInterface implements Outer.Inner
{
public void show()
{
System.out.println("This is show method implemented");
}
public static void main(String[]args)
{
Outer.Inner in=new TestInterface();
in.show();
}
}
Output:-
 C:\JAVATECH>javac TestInterface.java
C:\JAVATECH>java TestInterface
This is show method implemented

Example2:-TestInterface.java

interface Outer
{
void get();
interface Inner
{
void show();
}
}
class TestInterface implements Outer
{
public void get()
{
System.out.println("This is get method implemented");
}
public static void main(String[]args)
{
Outer in=new TestInterface();
in.get();
}
}
Output:-
 C:\JAVATECH>javac TestInterface.java
C:\JAVATECH>java TestInterface
This is get method implemented

Example3:-TestInterface.java

class Outer
{
interface Inner
{
void show();
}
}
class TestInterface implements Outer.Inner
{
public void show()
{
System.out.println("This is show method implemented");
}
public static void main(String[]args)
{
Outer.Inner in=new TestInterface();
in.show();
}
}
Output:-
 C:\JAVATECH>javac TestInterface.java
C:\JAVATECH>java TestInterface

This is show method implemented

Comments

Popular posts from this blog

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Object,Data Hiding

Object:- Object is real time entity exist in the real world , the basic purpose of object is to transfer data from one location to another location.object is small memory , allocated inside primary memory(RAM).in java object created inside heap memory,actually in java RAM memory  is divided into some segment according to the type of variables for example. RAM==>Heap area+Stack area+String constant pool+class area  etc. when object created then inside object only non static class variables occupied memory and initialize with default values. non static class variables also known as instance variable reside into object in heap area,static variables occupied memory in class area at the time of class loading from HARD disk to RAM and all local variables occupied memory in stack area when reaching to variables. object  created from the class then what object contains is depend on class structure or class members.             ...