• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

inner class

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).]
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
in u r answers 1 and 4 are the same. could u please rectify this.
Rahul.
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic