aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Direct access to class members Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Direct access to class members" Watch "Direct access to class members" New topic
Author

Direct access to class members

Venkat Ramanan
Ranch Hand

Joined: Jan 25, 2001
Posts: 30
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

Fawad Khan
Ranch Hand

Joined: Feb 13, 2001
Posts: 36
Hi,
For the time being no method is directly accessing the variable d. But i will work further and then explain.


Seeking Certification
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
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)
V Srinivasan
Ranch Hand

Joined: Aug 16, 2000
Posts: 99
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

Joined: Aug 14, 2000
Posts: 91
dear V Sri, please paste ur code ... we wd like to see where i am going wrong...
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
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).]
pankaj agarwal
Ranch Hand

Joined: Oct 13, 2000
Posts: 39
All the classes are in the same package. So, method of class B CAN access the member of class A.
Sowmya Vinay
Greenhorn

Joined: Feb 01, 2001
Posts: 24
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.
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
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

Joined: Feb 01, 2001
Posts: 24
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

Joined: Jun 30, 2000
Posts: 1070
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

Joined: Aug 16, 2000
Posts: 99
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

Joined: Aug 16, 2000
Posts: 99
Oooh, I became ranch hand now, what a surprize.
Thanks
mansoor iqbal
Ranch Hand

Joined: Aug 14, 2000
Posts: 91
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Direct access to class members