Click here to Skip to main content
15,120,078 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
suppose that i have class A(super class) having instance variable X

class B and class C is sub class of A ..
it is possible to that class B can access instance variable and class C can not???

if possible then how???

No, either both can access it or neither if both are subclasses of A. If you need something like that, you may want to rethink your inheritance.
Anand.genius 30-Jul-12 11:36am there anyother way to do so??
lewax00 30-Jul-12 11:42am
It depends on your needs. The simplest way would be to make another class that both A and C inherit from that does not have X, then move common functionality to that class instead of A. Or if A is abstract, just move X to class B.
Anand.genius 30-Jul-12 11:47am
ohk thanks ...
Sergey Alexandrovich Kryukov 30-Jul-12 13:02pm
Apparently so, my 5.
OP can always re-think the design, but learning OOP and the language comes first.
pasztorpisti 30-Jul-12 15:17pm
In java this can be perfectly valid design in some scenarios. See my answer.
Sergey Alexandrovich Kryukov 30-Jul-12 15:45pm
I did; and I up-voted it. Please see my comment below and the comment to your answer.
pasztorpisti 30-Jul-12 15:09pm
I think this solution isn't valid. You forgot about the default/package access modifier and packages that give more fine grained access control than the C++ modifiers!
EDIT: to be accurate, java doesn't have more fine grained access control, just different... :-)
Sergey Alexandrovich Kryukov 30-Jul-12 15:44pm
I up-voted this as well as your answer. To my excuse, I can say that this is not possible in general case; while your solution required certain distribution of the derived classes in different packages, one of them being the same as of the base class.
pasztorpisti 30-Jul-12 15:56pm
Sure! :) Its quite a specialized feature of java.
Yes its possible. Lets assume that class A has a member variable called m_Variable and it has default (package) access modifier. If class B is in the same package as class A and class C is in another package then B can access m_Variable while C cannot.

Here is the access level matrix from the original java tutorial:[^]
Subclasses can not see members with default/package modifier while classes in the same package can see it, even if they are not the subclass of the given class, but they can be.
Sergey Alexandrovich Kryukov 30-Jul-12 15:42pm
Good point, my 5. I did not think about different location of these two derived classes relative to the package of the base class. This solution is limiting, but this is a solution.
pasztorpisti 30-Jul-12 15:45pm
Thank you! Actually in java I used package default access modifier a lot. It rocks, provides the communication between your classes inside your package while it does not give permission for the users of the package. But the modifer of my member variables are strictly private in every language, I use more allowing modifiers only on member functions.
lewax00 30-Jul-12 15:46pm
I tried that out, and you are completely correct. It's a bit of a roundabout way to get there, but it does work. +5
pasztorpisti 30-Jul-12 15:51pm
Thank you! Actually its quite OK that there are no other ways to do it, because they would provide a base for really screwed up designs. Package default communication inside a package is very nice. Its sad that some java programmers dont really understand its use and they add public or protected access instead of this poisoning the public/protected interface.
CPallini 30-Jul-12 16:19pm
Good point.
Anand.genius 31-Jul-12 8:18am
thanks for solution ..i tried it works.......using package we can do this ......
pasztorpisti 31-Jul-12 8:23am
I'm glad I could help!
pasztorpisti 31-Jul-12 8:27am
BTW, this is possible even in C++ with the friend keyword but sharing private members too is quite a bruteforce approach to inter-package communication.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900