Thursday 13 January 2011

Protected doesn't protect

Remember C++. Remember OOP. Remember memory access modifiers. Remember the good old days, when private was private, protected was protected and public was public. What does private do? It tells a class to keep a variable/function to itself. What does protected do? It tells a class to keep a variable/function to itself and its subclasses. What does public do? It tells a class to expose a variable/function to everyone.

Enter Java. The great thing about Java is that this language is completely ass-backwards, has stupid naming convention, and yet it is somehow supposed to be an educational tool. First of all, what's up with all these "listeners" and "actions"? Whatever happened to event handlers and events? What the fuck is "extends" and whatever happened to "inherits"? Why the fuck they opted not to implement operator overloading, while at the same time doing exactly fucking that with a String class? What the fuck is "implements", and why use two different keywords to do essentially the same thing? And yet, they opted to ditch the concept of default parameters as it was "adding unnecessary complexity". Thanks guys, that made me real happy, writing all these overloads.

Another thing that bugs me all the way through Java development is that code of one class can't be separated into multiple files. Yeah, who gives a fuck about a class with 1000 lines of code, "when it's all in one place it's easier to comprehend". Why didn't they make it so i had to write ALL my code in one file then? That would certainly be easier to comprehend than wading through many files! All that "class should never have more than X lines of code" - cut the fucking crap, you never wrote a real program.

Now, the truly great part about Java is its memory access model. Java did introduce an interesting concept, called packages. I understand that concept and i like it. What i don't understand is why did they have to fuck with traditional notions of "public", "private" and "protected". I mean, i don't mind adapting these to the fact that Java now has packages, but why dealing with this in such an illogical manner?

Consider this. We have a package, and we can declare classes as either private or public. What does that mean? Class declared as public would be visible from outside the package. Class declared as private would be visible only inside the package. Now why would i need protected classes? Java gives us the answer - in case your class has another, inner class. Now this is complete bullshit.

First of all, why make an inner class in the first place? Whatever happened to the "one file - one class" paradigm? Second, that was a rhetorical question, because i do actually understand why would i need an inner class. What i don't understand, however, is that if i don't want this class to be seen from outside its outer class, i would make it private. If i want subclasses to have access to that inner class, i would make it protected. If i want other classes to access it, i would make it public. That doesn't deal with the fact that if i want other classes to have access to it i would not make it inner in the first place, but we'll leave that fact aside.

But what does this have to do with packages?! Why the scope of the memory access modifier goes beyond the borders of the class? In my opinion, Java access modifiers should have been like this:

Package level - public (visible outside the package), private (not visible outside the package). No protected, because it doesn't make sense.
Class level - public (visible outside the class), protected (not visible outside the class but visible to subclasses) and private (not visible outside the class).

That's fucking it! No fuss, no headaches, dead fucking simple. Why Java changed the behaviour of these basic things so that they actually operate on the package level is beyond me.

Now let's see if it would actually work. The famous Java visibility table:





Access Levels
Modifier Class Package Subclass World
public Y Y Y Y
protected Y Y Y N
no modifier Y Y N N
private Y N N N


What the fuck is "no modifier"? Whatever happened to "private by default" OOP paradigm? Let's try and make this table from my perspective:




Access Levels
Modifiers Class Package Subclass World
public class, public member Y Y Y Y
public class, protected member Y N Y N
public class, private member Y N N N
private class, public member Y Y Y N
private class, protected member Y N Y N
private class, private member Y N N N


That's it! Obviously i generally don't quite get the concept of package visibility and why does one need any. Package visibility should be controlled with the same logic class visibility is - by using either private or public, thus making stuff either invisible or visible from outside of the package. Creating a backdoor for accessing members from inside the package while still denying access to them from a subclass is as idiotic as it can be - shouldn't subclass have higher priority of access than package?

Overall, Java model of access modifiers is illogical, inconsistent, and it just plain sucks.

No comments:

Post a Comment