| Author |
MOCK help
|
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
I am feeling MOCK answer is incorrect.On object o1 of class A member of class B can be available only thorough inheritance not using dot.Please help which one is correct mine or mock.
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1237
|
|
Shalini,
Mock answer seems to be correct. Because if you don't specify any access specifier it's default i.e package level. Within the same package it can be accessed by any means. Doesn't matter whether it's inherited or not. Correct me if I am wrong.
|
Swastik
|
 |
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
|
On the object o2 of class B in class A we can access class B members without inheritance using dot but without inheritance how members of class B would be accessible on object o1 of class A ?
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 4389
|
|
A little bit code to show the mock answer is absolutely correct. Class A accesses a member (without access modifier) of class B. class A and B are in same package and no inheritance is needed.
This code compiles successfully.
Move class B to another package and see what happens...
|
SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 4389
|
|
Shalini Srivastav wrote:but without inheritance how members of class B would be accessible on object o1 of class A ?
By simply creating an instance, like shown in my previous post.
|
 |
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
My imagination was ..
|
 |
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
|
Do you think same ?
|
 |
Swastik Dey
Ranch Hand
Joined: Jan 08, 2009
Posts: 1237
|
|
|
I don't think the above code has go anything to do with access specifier. It won't compile only because the variable is not member of that class.
|
 |
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
Swastik Dey wrote:I don't think the above code has go anything to do with access specifier. It won't compile only because the variable is not member of that class.
Yes, so it will be possible only through inheritance in same pkg,which was my answer.
|
 |
Roel De Nijs
Bartender
Joined: Jul 19, 2004
Posts: 4389
|
|
Shalini Srivastav wrote:Yes, so it will be possible only through inheritance in same pkg,which was my answer.
That's indeed true for the code sample you have created,
But lines 12 and 13 from your code snippet show that you can access from class A a pacakge-private (no access modifier) member of class B. If you need to access the member of class B through an instance of class A your answer (same package + inheritance) would be correct
|
 |
Shalini Srivastav
Ranch Hand
Joined: Jul 21, 2012
Posts: 86
|
|
Roel De Nijs wrote:
Shalini Srivastav wrote:Yes, so it will be possible only through inheritance in same pkg,which was my answer.
That's indeed true for the code sample you have created,
But lines 12 and 13 from your code snippet show that you can access from class A a pacakge-private (no access modifier) member of class B. If you need to access the member of class B through an instance of class A your answer (same package + inheritance) would be correct
I got confused because i thought that there must be some use of object o1 of class A because it is mentioned so i was using it to access class B's members on it using inheritance but i think it has no mean here.We are simply accessing members on Object o2 of class B in class A.
|
 |
 |
|
|
subject: MOCK help
|
|
|