user1925406
user1925406

Reputation: 733

Inner and Nested Classes

Oracle documentation(in below link) says that:

Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private.

But in below example I created an object objin (of inner class) and it couldn't access any of the method or variable of its enclosing outer class. Below is the code, could you any one clarify on the same?

http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

package Package_1;
public class Outer {

    int k;
    public void Multiply()
    {
        System.out.println("inside outer class' method multiply");
    }

    public class Inner {
        int l;
        public void Division()
        {
            System.out.println("inside inner class' method Divison");
        }

    }
}

Class with Main method

package Package_1;

public class D {

    public static void main(String[] args) {
        Outer objout = new Outer();
        objout.k = 5;
        objout.Multiply();

        Outer.Inner objin = objout.new Inner();
        objin.l = 7;
        objin.Division();          
    }
}

With objin object I couldn't access that Multiple method in its enclosing class.

Upvotes: 2

Views: 573

Answers (4)

Jops
Jops

Reputation: 22715

I see your "privates"!

From the code of the non-static nested class (inner class) you have access to both public and private members of the enclosing class.

enter image description here

But it's your "privates", not mine!

This is what you're thinking when you try do objin.Multiply(): you are accessing Multiply() as though it's a member of the inner class, but it's not. Remember, you can see it from within the code of the inner class, but it will not be exposed as though it's yours.

enter image description here

Upvotes: 5

Nishant Lakhara
Nishant Lakhara

Reputation: 2445

You are trying to access the method by using instance of the Inner class object. It can be accessed outside only by Outer class method. You can call the method inside the class definition of the Inner class directly but not by using the instance of inner class. if you still want to do this try :

package Package_1;
public class Outer {

int k;
public void Multiply()
{
    System.out.println("inside outer class' method multiply");
}

public class Inner {
    int l;
    public void Division()
    {
        System.out.println("inside inner class' method Divison");
    }

    public void Multiply() {
         Outer.this.Multiply();  //Outer class method.
    }

}
}

Upvotes: 0

Narendra Pathai
Narendra Pathai

Reputation: 41945

This is what the specification says

public class Outer{
   private int x;
   private void method(){
   }
   public void publicMethod(){} 

   public class Inner{

      //has access to even private properties of x
      method(); //can be called

      //NOTE: Only has ACCESS and does not INHERIT those methods
   }

}

What you are trying is to access the publicMethod() with the instance of Inner, which does not have any method of that name.

THE CRUX:

The non-static nested classes just has the access to all properties and methods of container class, but does not inherit those methods.

objin.Multiply() cannot work as the crux explains that Multiply() is defined on Outer and not Inner, so there is no method Multiply() on Inner

Upvotes: 2

JB Nizet
JB Nizet

Reputation: 691745

The documentation doesn't say that you can access to fields and methods of the outer class by using a reference to the inner class. So you can't do

objin.Multiply();

because Multiply is not a method of Inner. What you can do is:

public class Inner {
    int l;
    public void Division()
    {
        System.out.println("I can access the field k in outer: " + k);
        System.out.println("I can access the method Multiply in outer (even if Multiply was private): ");
        Multiply();
        // which is a shortcut for
        Outer.this.Multiply();
    }
}

PS: please respect the Java naming conventions. Methods start with a lowercase letter.

Upvotes: 1

Related Questions