aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class" Watch "inner class" New topic
Author

inner class

hema janani
Ranch Hand

Joined: May 19, 2000
Posts: 70
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

Joined: Aug 28, 2003
Posts: 2
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

Joined: Nov 22, 2008
Posts: 18944
hi,
in u r answers 1 and 4 are the same. could u please rectify this.
Rahul.
Keyur Shah
Greenhorn

Joined: May 31, 2000
Posts: 15

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

Joined: May 19, 2000
Posts: 70
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


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: inner class