• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Direct access to class members

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In classes A, B, and C, which methods have direct access to A.d?

Class A {
protected double d=3.14;
public static void f() {};
public void g() {};
}
Class B {
public void m() {};
}
Class C extends A {
protected static void s() {};
void t() {};
}
a) A.f

b) A.g

c) B.m

d) C.s

e) C.t

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
For the time being no method is directly accessing the variable d. But i will work further and then explain.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have tried all the possibilities and:
the program wont compile
get the same erroe: can't make a static ref to a nonstatic variable d in class A
i used System.out.println(A.d)
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
My answer is a, b and e. Class B is doing nothing in this question. Protected variable, methods can be accessed by a sub-class, so protected variable d is accessible in Class C which is extending Class A and it can be used in any type of methods in Class C except static becos static method can access outside static variable and var declared within the method. Ofcourse Class A has verymuch access to var d.
Thanks
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dear V Sri, please paste ur code ... we wd like to see where i am going wrong...
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I think:
A. False - d is not static, and method f() is static. Static methods cannot access non-static methods or varialbes.
B. True - Neither d or g() are static and they are in the same class so modifiers don't matter.
C. False - Class B has no relation to Class A. This would work if you created an instance of A and then referenced d with that instance, but you can's say A.d because d is not static.
D. False - s() is static, same rule as answer A.
E. True - t() is not static and d() is protected which gives access to subclasses and classes in the same package. C is a subclass of A, so you have access to d.
Now, you can never access the variable like A.d, because d is not static, and that may be what you are doing in your code mansoor. But I think the question is which ones can access d without an instance of A, but it is not entirely clear.
My answer though would be B and E.
mansoor, change your code to say:
System.out.println(d);
and see what you get?
Bill
[This message has been edited by bill bozeman (edited February 13, 2001).]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All the classes are in the same package. So, method of class B CAN access the member of class A.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by V Srinivasan:
Hi,
My answer is a, b and e. Class B is doing nothing in this question. Protected variable, methods can be accessed by a sub-class, so protected variable d is accessible in Class C which is extending Class A and it can be used in any type of methods in Class C except static becos static method can access outside static variable and var declared within the method. Ofcourse Class A has verymuch access to var d.
Thanks


I am interpreting direct access as accessing members directly without creating an instance of their class.
Based on this ,the answers are b and e.
The choice b is correct since A.g() being a non-static method can access d which is also non-static.
The answer e is right, since C is a sub class of A and a protected member of a class is accessible across all its subclasses irrespective of whether they are in same package or not. Note that within the non-static method of class C, the inherited variable d of superclass A can be directly accessed.
The choice a is not an answer because A.f() is a static method and d is a non-static variable. Static methods cannot reference non-static members of its class.
The choice c is not correct, since class B is not related to class A. However the method m of class B can access variable d of class A via an instance of class A, if class B and class A are in the same package.
( something like this can be done in class B:
public void m(){
A a = new A();
a.d=14.5;
}
However, if class B doesn't belong to the same package as class A then this possibility is completely ruled out!
NOTE: Also, since variable d is a non-static member it cannot be referenced as A.d . So Mansoor Iqbal's System.out.println(A.d); won't compile
)
The choice d is not correct since the method s of class C being a static method cannot reference non-static inherited member d. Please note that the variable d of class A can be accessed through an instance of class A here. i.e something like that mentioned in the above example can be done irrespective of whether C and A are in the same package or not since C is a subclass of A!
Hope this helps,
A suggestion: in order to find out solutions, try compiling the code yourself. Believe me, you will find answers easily and your comprehensive ability of basic concepts will be strengthened if you do it yourself!!
Sowmya Vinay.
 
Ranch Hand
Posts: 625
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see any indication that those three classes are in the same package. There isn't a package statement that says that they are, so I don't think that we can assume that they are.
 
Sowmya Vinay
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow, by the time I typed my reply, Bill has already given the solution. Its really amazing, how many people are simultaneously working to solve a problem thanks to Javaranch. Keep it up GUYS!!
 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sean, if there is no package name then they are using the default package. We are assuming that they are in the same directory though.
And Sowmya, I agree with you. It is amazing how similar are responses are.
Bill
 
V Srinivasan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Bill,
Oh, what a blunder I did. I correct myself, my aswer is B & E.
Thank you very much.
[This message has been edited by V Srinivasan (edited February 14, 2001).]
 
V Srinivasan
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oooh, I became ranch hand now, what a surprize.
Thanks
 
mansoor iqbal
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi bill:
thank u. best solution.
swomy: u hit the nail on the head: defined direct access......i think most of the time one needs to state the problem very carefully...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic