Hi,
About protected methods: a protected method is available to sub-classes of the declaring class, and to all other classes that belong to the same package.
Well, this is a basic Java definition, and it works quite well.
Now my problem is the following, how can I block the access to any others but the sub-classes? Any trick to do that?
Let's say I create a protected (and final) method that will be called from the main class itself. Since I want the implementation to be required, on the main method I create it as an abstract method. So anyone that extends the class will have to implement the method. Perfect! But then again, I don't want anyone to be able to call it externally ... the question is, I cannot (or can I?) avoid people to create an external (totally independent) class, put it on the same package, and call the method directly.
So I could develop a framework for some job, distribute as a single external jar file, but people could still create a class on the same package and directly access the protected methods. There are some partial solutions on my mind to control this (nothing concrete yet), but I was hopping to also get some opinions from this forums.
Is there any trick to avoid that?
About protected methods: a protected method is available to sub-classes of the declaring class, and to all other classes that belong to the same package.
Well, this is a basic Java definition, and it works quite well.
Now my problem is the following, how can I block the access to any others but the sub-classes? Any trick to do that?
Let's say I create a protected (and final) method that will be called from the main class itself. Since I want the implementation to be required, on the main method I create it as an abstract method. So anyone that extends the class will have to implement the method. Perfect! But then again, I don't want anyone to be able to call it externally ... the question is, I cannot (or can I?) avoid people to create an external (totally independent) class, put it on the same package, and call the method directly.
So I could develop a framework for some job, distribute as a single external jar file, but people could still create a class on the same package and directly access the protected methods. There are some partial solutions on my mind to control this (nothing concrete yet), but I was hopping to also get some opinions from this forums.
Is there any trick to avoid that?