Click here to Skip to main content
15,886,110 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???
Posted

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.
 
Share this answer
 
Comments
Anand.genius 30-Jul-12 11:36am    
thanks...is 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.
--SA
pasztorpisti 30-Jul-12 15:17pm    
In java this can be perfectly valid design in some scenarios. See my answer.
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: http://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html[^]
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.
 
Share this answer
 
v3
Comments
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.
--SA
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.

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