• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class

 
hema janani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Que fro Barry Boone
class A{
public int x;
private int y;
class B{
protected void method1(){
}
class c{
private void method2(){
}
}
}
}
what can method2() access directly,without a reference to another instance?select all valid ans.
a. the variable x defined in A
b. the variable y defined in A
c. method1 defined in B
d. the variable z defined in D
ans is a,b,c
please explain how in detail?
there is a lot of confusion on this topic.
I always make a mess of prob. of inner class.could someone highlight on the rules to keep in mind while solving the problem on inner classes.
please help.

[This message has been edited by hema janani (edited June 10, 2000).]
 
Parag Gokhale
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since method2 is defined in the inner class C, all the variables and methods present in the super classes are visible to it. hence method1 in Class B, and variable X (public) and variable Y ( private) are also visible to it, since they in turn are visible to class B. am i clear. however the vice-versa is not clear.

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
in u r answers 1 and 4 are the same. could u please rectify this.
Rahul.
 
Keyur Shah
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Well.. I don't think answer 'c' could be right. Class B & Class C can directly refer member varible (nonstatic) of Class A and member methods (which is not there in this case )
So the point is - without creating an instance of inner Class - B we could not call the method defined inside the class on Class - C.
- Keyur
 
hema janani
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by parag:
Since method2 is defined in the inner class C, all the variables and methods present in the super classes are visible to it. hence method1 in Class B, and variable X (public) and variable Y ( private) are also visible to it, since they in turn are visible to class B. am i clear. however the vice-versa is not clear.

thankyou parag
The answer for above is a,b,c.becoz inner class can access all the var./methods of an enclosing class.this is the 1st rule.
and a 2nd rule:an inner class cannot access a var. in a sub class of an enclosing class without a reference to it,so d is not correct.could anyone tell me which book has mentioned the above 2nd rule.
hema


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic