Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access modifiers in Java compared to c++ [closed]

I've seen some discussions in StackOverflow about this subject, but I didn't see something that helped me understand the following point:

I'm coming from C++ background and lately I started to learn Java. In C++ when protected is used only a subclass can access the member (the analog to Field in Java).

In C++ there is also the "friend" classes that can have access to private/protected mambers of the class that giving "friendship". This is little bit analogous to "package" field modifier in Java (default field modifier), except that in C++ a friendship gives access to all private members, but in Java the access from classes in the same package is specific for a class field.

What I couldn't understand is, assuming that I want to give access only to subclasses, this is something I can do in C++ by declaring the members protected in a class that doesn't "give" friendships.

But in Java, I don't know how can I do it, since by using "protected" field modifier - I also give access to all classes in the package. The only way that I find to do it is to declare the field protected and have the class isolated in its package.

From here, what I conclude is that the grouping of classes in one package must be done on basis of "friendship" between the classes. Is this indeed the leading consideration in package grouping?

Another thing I don't understand, In Java, assuming I have two fields in the class A: b,c. I want to give B access to b but not to c, and I want to give C access to c but not to b. and to the "World" I want b,c to be hiden. How can it be done? I guess B,C should be both in the same package as A. but by declaring b,c with package modifier I let B,C access both to b and c. Is there a way in Java to do it?

Hope for some explanation of this subject

like image 302
Day_Dreamer Avatar asked Sep 28 '22 14:09

Day_Dreamer


2 Answers

In C++ when protected is used only a subclass can access the member (the analog to Field in Java).

Access specifiers are also for member functions / methods, not just member variables / fields.

In C++ there is also the "friend" classes that can have access to private/protected mambers of the class that giving "friendship". This is little bit analogous to "package" field modifier in Java (default field modifier), except that in C++ a friendship gives access to all private members, but in Java the access from classes in the same package is specific for a class field.

There are not only friend classes but also functions.

It's true that Java's package-private access is similar, but it's not a complete replacement. A better way to put it would be that those two features have a subset of problems they solve. There are problems that can be solved by friend but not by package-private, and vice versa.

What I couldn't understand is, assuming that I want to give access only to subclasses, this is something I can do in C++ by declaring the members protected in a class that doesn't "give" friendships.

But in Java, I don't know how can I do it,

The answer is: You cannot.

since by using "protected" field modifier - I also give access to all classes in the package.

Exactly.

The only way that I find to do it is to declare the field protected and have the class isolated in its package.

Technically, yes. But this creates other problems. Your class will no longer be able to access package-private elements of its previous package. Let's say your BaseClass used to be in com.example.one. You move it to com.example.two. Now it will no longer be able to access other package-private classes of com.example.one.

Is this indeed the leading consideration in package grouping?

Yes, Java is designed like this. You can try to fight the language rules, but that's a losing battle in any programming language.

Another thing I don't understand, In Java, assuming I have two fields in the class A: b,c. I want to give B access to b but not to c, and I want to give C access to c but not to b. and to the "World" I want b,c to be hiden. How can it be done?

It cannot be done in a clean way (by clean I mean: without any hacks that would require you to inspect the call stack at runtime and throw exceptions).

If you are concerned about this scenario because you are designing a public API, a low-tech solution which usually works perfectly is to create one or more *.internal packages and clearly document the fact that those are not supposed to be used in client code.

like image 74
Christian Hackl Avatar answered Oct 07 '22 17:10

Christian Hackl


Those are quite a bunch of questions together...

But in Java, I don't know how can I do it, since by using "protected" field modifier - I also give access to all classes in the package.

True, there's no way to give access only to subclasses but not to classes in the same package. It was a design decision taken ages ago...

The only way that I find to do it is to declare the field protected and have the class isolated in its package.

This is technically correct, though it would be of little usage. Packaging of classes is meant to be used for grouping related classes, where 'related' means "classes that fulfil a specific relation", i.e. they belong to the same use case, belong to the same architectural layer, are in charged of the same entity, etc.

From here, what I conclude is that the grouping of classes in one package must be done on basis of "friendship" between the classes. Is this indeed the leading consideration in package grouping?

I believe I have already answered this in the preceding paragraph: packaging is meant to group related classes according to some specific criteria.

For your A, B and C classes example with attributes:

I guess B,C should be both in the same package as A. but by declaring b,c with package modifier I let B,C access both to b and c. Is there a way in Java to do it?

The answer is no, there's no simple, clean way to do it. You could achieve it with some hack or some more advanced techiques, but, again, this was part of the decisions taken by language designers ages ago...

like image 38
fps Avatar answered Oct 07 '22 17:10

fps