Discussion:
Protected class member
(too old to reply)
JiiPee
2016-04-27 15:25:51 UTC
Permalink
In C++ protected members can only be called from inherited classes. In
java they can also be called from package-classes, even if not inherited.

My questions is, that if I want only the inherited classes to see the
protected members what is the right way to do it? Is it that I have to
put them into a separate package? I have to create a new package and do
it there? Is this the normal way to do this?
c***@gmail.com
2019-07-31 16:09:02 UTC
Permalink
Post by JiiPee
In C++ protected members can only be called from inherited classes. In
java they can also be called from package-classes, even if not inherited.
My questions is, that if I want only the inherited classes to see the
protected members what is the right way to do it? Is it that I have to
put them into a separate package? I have to create a new package and do
it there? Is this the normal way to do this?
Jii,

I think there is no easy direct answer to this. Protected access in Java is not the same as in C++, which does not have packages. In C++ protected applies to classes. The tricky thing is that in Java protected does not directly apply to class. A good practice when designing classes in either language is to favor private, and expose only member method/functions as public.

If this doesn't facilitate the requirements, then in C++ open access to ancestors through protected. In Java use a package class to access default or protected attributed or method members as needed.
Graeme Geldenhuys
2019-08-14 17:51:01 UTC
Permalink
Post by JiiPee
In C++ protected members can only be called from inherited classes. In
java they can also be called from package-classes, even if not inherited.
Then move that class into a separate package. Simple as that. ;-)

Regards,
Graeme

c***@gmail.com
2019-07-31 16:15:16 UTC
Permalink
Post by JiiPee
In C++ protected members can only be called from inherited classes. In
java they can also be called from package-classes, even if not inherited.
My questions is, that if I want only the inherited classes to see the
protected members what is the right way to do it? Is it that I have to
put them into a separate package? I have to create a new package and do
it there? Is this the normal way to do this?
Jii,

I think there is no easy direct answer to this. Protected access in Java is not the same as in C++, where packages behave differently. In C++ protected applies to classes. The tricky thing is that in Java protected does not directly apply to class. A good practice when designing classes in either language is to favor private, and expose only member method/functions as public.

If this doesn't facilitate the requirements, then in C++ open access to ancestors through protected. In Java use a package class to access default or protected attributed or method members as needed.
c***@gmail.com
2019-07-31 16:16:20 UTC
Permalink
Post by JiiPee
In C++ protected members can only be called from inherited classes. In
java they can also be called from package-classes, even if not inherited.
My questions is, that if I want only the inherited classes to see the
protected members what is the right way to do it? Is it that I have to
put them into a separate package? I have to create a new package and do
it there? Is this the normal way to do this?
Jii,

I think there is no easy direct answer to this. Protected access in Java is not the same as in C++, where packages behave differently. In C++ protected applies to classes. The tricky thing is that in Java protected does not directly apply to class. A good practice when designing classes in either language is to favor private, and expose only member method/functions as public.

If this doesn't facilitate the requirements, then in C++ open access to ancestors through protected. In Java use a package class to access default or protected attributed or method members as needed.
Continue reading on narkive:
Loading...