0

There was one MCQ ( Multiple Choice Question ) while I was reading my Java study book and that MCQ is:

Question : In case of inner and outer classes, _________

Options are :

(a) The members of outer class can not be accessed by inner class.

(b) The members of inner class can not be accessed by outer class.

(c) Members of both can be accessed by both of the classes.

(d) None of these.

The answer given on book answer key is (b) but I'm not feeling it as right answer because outer class can access members of its inner class I think. So please help me with what is right.

Thanks, have a good day :)

RubioRic
  • 2,442
  • 4
  • 28
  • 35
Natha Odedara
  • 206
  • 3
  • 9
  • Did you try writing a simple test case? Please do at least that before posting here so there's a concrete example to work with. – Jim Garrison Apr 04 '16 at 05:13
  • @JimGarrison Multiple Choice Question. I have edited. Pending approval. – RubioRic Apr 04 '16 at 05:14
  • @JimGarrison MCQ is just a question with Multiple Choice Question (MCQ). And we have to just pick answer from the given options. – Natha Odedara Apr 04 '16 at 05:14
  • @JimGarrison I know I am familier a little with basics but as the answer in the book was given a (b). I just want people from here to confirm because I feel the trust on people here more than a book. – Natha Odedara Apr 04 '16 at 05:16
  • 2
    No, _EXPERIMENT_ with your IDE. You will learn much better that way than just having a question answered. – Jim Garrison Apr 04 '16 at 05:17
  • This might help you : http://stackoverflow.com/questions/12251922/i-thought-inner-classes-could-access-the-outer-class-variables-methods. – Helios Apr 04 '16 at 05:17
  • @Nik Thanks buddy. Got my answer from there. Thank you again. – Natha Odedara Apr 04 '16 at 05:20
  • @NathaOdedara still if you want to learn , get in hapit of hands-on . Nothing is better than that. Refer books, blogs peoples for knowledge but to be confirm and develop skill you need to get your hands dirty. Use stack when you have exact piece of code buging you. And do remember "CODING/PROGRAMMING IS SKILL LIKE SWIMMING , YOU CANNOT LEARN ONLY FROM INSTRUCTOR'S EXPERIENCE OR BOOKS , YOU NEED TO ENTER IN WATER". – Panther Apr 04 '16 at 05:23
  • @Panther Yes I know that Coding can be only improved by practicing yourself. But now I had no any other option than posting here and to do it practically myself otherwise I could have done it. Hope you understand. – Natha Odedara Apr 04 '16 at 05:27
  • 2
    http://ideone.com/qsbAzh – Radiodef Apr 04 '16 at 05:30

2 Answers2

0

Sorry for the confusion.

You can access inner and outer classes both ways. I do suggest trying a simple example though yourself as programming is one of those things you only learn through your own problems.

Refer to this as this may help: Can an outer class access the members of inner class?

Community
  • 1
  • 1
Porteous96
  • 97
  • 2
  • 11
  • 1
    You're saying "outer classes cannot be accessed from an inner class"? Please post an example, because I think this is wrong or else I'm not understanding what you're saying correctly. – ajb Apr 04 '16 at 05:36
0

lets make it simple with some code

public class A {
    public int a = 1;
    public class B {
        public int b = 2;
        public int getAfromB() { return a; } // ACCESS OUTER CLASS MEMBER IMPLICITLY
        public int getBfromB() { return b; }
    }
    public int getBfromA() {
        B myB1 = new B();
        B myB2 = new B();
        return myB1.b + myB2.b; 
    }
}

An B instance is linked to a specific A instance, it belongs to the instance scope. In its scope, the members of the A class are defined.

The A class can handle several instances of the B class. It will be able to manipulate them but cannot implicitly access a specific instance members, simply because 'b' is not unique from its perspective.

Ji aSH
  • 3,206
  • 1
  • 10
  • 18