» » Inner classes in java

  • Class inside a class is known as nested class.
  • We will use nested classes to group classes and interfaces at one place for readability and maintainability.
  1. package com.instanceofjava;
  2. public class A{ 
  3.  class B{
  4. // inner class
  5. }
  6. }


  • Inner class is a part of nested class. Non-static nested classes are known as inner classes. 
  • Nested classes are two types
  • Non-static nested class(inner class)
    1. Member inner class
    2. Anonymous inner class
    3. Local inner class
  • Static nested class.

Member inner class:

  •  A class is defined within a class and outside of methods of that class known as member inner class.
  1. package instanceofjavaforus;
  2. public class A{ 
  3.  class B{
  4. public void show(){
  5. }
  6. }
  7. public static void main(String args[]){
  8. A a= new A();
  9. a.B b= a.new B();
  10. b.show();
  11. }
  12. }

 

Anonymous inner class:

  • Anonymous inner class is a inner class which does not have a name and whose instance is created at the time creating class itself.
  • Anonymous inner classes are somewhat different from normal classes in its creation.
  • There are two ways to create in two ways.
    1. Using class.
    2. Using interface.

Using class:

  1. package instanceofjavaforus;
  2. public class A{ 
  3. public void show(){
  4. }
  5. public static void main(String args[]){
  6. A a= new A(){
  7.  public void display(){
  8. System.out.println('anonymous class method');
  9. }
  10. };
  11. a.display();
  12. }
  13. }


  Using interface:

  1. package instanceofjavaforus;
  2. public interface A{ 
  3.  public void show();
  4. }

  1. package instanceofjavaforus;
  2. public class Demo{ 
  3. public static void main(String args[]){
  4. A a= new A(){
  5.  public void show(){
  6. System.out.println('anonymous class method');
  7. }
  8. };
  9. a.show();
  10. }
  11. }

 

Local inner class:

  • A class which is defined inside a method of another class known as local inner class
  1. package instanceofjavaforus;
  2. public class Demo{ 
  3. void create(){
  4. class A{
  5. void show(){
  6. System.out.println("local inner class method");
  7. }
  8. }
  9. A a= new A();
  10. a.show();
  11. }
  12. public static void main(String args[]){
  13. Demo obj= new Demo();
  14.  obj.create();
  15. }
  16. }

 

Static nested class:

  • A static nested class is a class which is defined inside a class as static.
  1. package instanceofjavaforus;
  2. public class Outer{ 
  3.   static class inner{
  4. public void show(){
  5. System.out.println("static inner class method");
  6. }
  7. }
  8. public static void main(String args[]){
  9. Outer.inner in= new Outer.innner();
  10. in.show();
  11. }
  12. }

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu