Inner class

I took these examples from Vinod Pillai lecture on youtube but I think there may be mistakes in his explanations

Inner classes

package main;

public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        OuterClass outObj = new OuterClass();
      
        // to access the Inner class, we must use the OuterClass object
        OuterClass.InnerClass inObj=outObj.new InnerClass();
        
        inObj.getValue();
        
        outObj.accessInner();
         
    }
}


class OuterClass
{
    private int x=200;
    
    // the INNER CLASS //////////////////////////////////
    public class InnerClass
    {
        private int ans;
        
        public void getValue()
        {
            display();
            System.out.println("Outer class value= "+x);
        }
        
        public void display()
        {
            System.out.println("Inner class display");
            OuterClass.this.display();
        }
    }
    /////////////////////////////////////////////////////
    
    public void display(){
        System.out.println("Outer class display");
    }
    
    public void accessInner()
    {
        InnerClass obj = new InnerClass();
        obj.ans=300;
        
        System.out.println("Inner class value= "+obj.ans);
    }
}

Outputs:

Inner class display
Outer class display
Outer class value= 200
Inner class value= 300

Modifiers

Inner classes, unlike the outer class, CAN have modifiers:

final / abstract/ public/ private/ protected & static

Inner classes within methods

package main;

public class Main {

    public static void main(String[] args) {
        // TODO code application logic here
        OuterClass outObj = new OuterClass();
      
        outObj.methodContainingInnerClass();
         
    }
}


class OuterClass
{
    private int x=200;
    
    public void methodContainingInnerClass()
    {   
        int rel=800;
        
        // inner class in a method can only be final or abstract
        class InnerClass{
            
            private int ans;
            
            public void getValue()
            {
                // the inner class within a method, CAN access
                // the variables and methods of the outer class
                System.out.println("Outer class value= "+x);
                printSomething();
                
            }
        }
        
        // to access an inner class in a method, you HAVE to
        // so this within the method itself, there is no
        // other way of accessing it as it's scope belongs in the 
        // method
        new InnerClass().getValue();
    }
    
    public void printSomething()
    {
        System.out.println("Here's something "+ (x+50));
    }
}

Outputs:

Outer class value= 200
Here’s something 250

Anonymous inner classes

public class Main {

    public static void main(String[] args) {
        
        InnerClass inObj = new InnerClass();
      
        inObj.obj.display();
    }
}


class OuterClass
{
  public void display()
  {
      System.out.println("Outer class");
  }
}

class InnerClass
{
    OuterClass obj=new OuterClass()
    {
        public void display()
        {
            System.out.println("Anonymous class");
        }
    };
}

Outputs:

Anonymous class

However if we change line 24 to

public void display1() , we are no longer overriding the display() method and the OuterClass display() method is called, e.g.

public class Main {

    public static void main(String[] args) {
        
        InnerClass inObj = new InnerClass();
      
        inObj.obj.display();
    }
}


class OuterClass
{
  public void display()
  {
      System.out.println("Outer class");
  }
}

class InnerClass
{
    OuterClass obj=new OuterClass()
    {
        public void display1()
        {
            System.out.println("Anonymous class");
        }
    };
}

Outputs:

Outer class

What about another method in the anonymous class, how do we call it??

e.g.

package main;

public class Main {

    public static void main(String[] args) {
        
        InnerClass inObj = new InnerClass();
      
        inObj.obj.display();
        
        inObj.obj.display1(); // THIS WON'T WORK !!!!!!!!!!!!
    }
}


class OuterClass
{
  public void display()
  {
      System.out.println("Outer class");
  }
}

class InnerClass
{
    OuterClass obj=new OuterClass()
    {
        public void display()
        {
            System.out.println("Anonymous class");
            // display1(); // this COULD call the other method though

        }
        
        // IF we have another method in the anonymous class
        // as below, (see also !!!!!!!!!!!! in main method
        public void display1()
        {
            System.out.println("display1 method in anonymous class");
        }
    };
}

Here we get a compilation error, we CANNOT access display1() in the anonymous method
using:

inObj.obj.display1();

because

inObj.obj will reference the parent class (OuterClass) (polymorphism)

However we could call it from line 31 (commented out currently)

Static modifier with inner classes

package main;

public class Main {

    public static void main(String[] args) {
        
        OuterClass.InnerClass obj = new OuterClass.InnerClass();
        obj.display();
    }
}


class OuterClass
{
  int rel=800;
  
  static class InnerClass
  {
      public void display()
      {
           System.out.println("InnerClass (static) ");
            
           // However the following line won't now be able to access rel
           //System.out.println("InnerClass (static) "+rel);
      }
  }
}

Leave a Reply