Tuesday, August 10, 2010

In the protected way...

You may wonder what do I mean. No, I'm certainly not gonna write about driving a bullet proof limo or wearing this kind of vest. Protected sex? No, not really. Of course it's not like I'm supporting having unprotected sex but just not this time, ok? So, here it comes 3 2 1... protected modifier! Yes, today we will talk about the protected modifier in Java. Yeah I know, you may have expected something else, like the things I mentioned before.
So let's go! At the beginning let me recap the default (package) modifier. To use this one we don't put anything in front of the member of a class, like in the following example:
 package example;
class DefaultAccess {
int a;
}

By this the member 'a' has the package access. That means that any other class in the package 'example' (or any other that we define) can access the member 'a' by a dot operator. And it's legal only in this package. A try to access it from any other package will not work. No exceptions. No excuses.

All right, that's pretty straightforward but this article is about the protected se... protected modifier it is! Okay. A protected modifier works in the same way as a default modifier as for the dot operator. So you can access a protected member within the same package with no problem. But outside it, you can forget. Well, nearly. You can actually make some sort of magic to access a protected member in the different package. And this magic is... inheritance! Okay, not a lot of magic in it. So, what we can do to access it, is to extend the class that has a protected member and then within its methods we can work around. Let's have a look at this example:
package protectedExample;

public class Loo {
protected int a;
}

package protectedExampleDifferent;

import protectedExample.Loo;

public class Woo extends Loo{
void callWoo() {
System.out.println(a);
}
}

That's how it works. First, we extend the class with protected member and then we can access it as in the normal inheritance. But how does it look for other classes that use the Woo class. Can they access the inherited member? The answer is no. For any other class, regardless to the package (of course excluding the one that the protected member is defined in) the protected inherited member is not visible. If we extend the previous example we can get:
package protectedExampleDifferent;

import protectedExample.Loo;

public class Woo extends Loo{
void callWoo() {
System.out.println(a);
}
}

class Woo2 {
void test() {
Woo woo = new Woo();
System.out.println(woo.a);
}
}
And here it's not gonna work. Even though the member 'a' is inherited and can be used within the Woo class, it can't be accessed by the dot operator by another class.
But this is not the end! We can still extend our class Woo and guess what! The member 'a' will be inherited! Let's slightly modify the previous example again:
package protectedExampleDifferent;

import protectedExample.Loo;

public class Woo extends Loo{
void callWoo() {
System.out.println(a);
}
}

class Woo2 extends Woo {
void test() {
System.out.println(a);
}
}

That's pretty savvy but that's how it works. How useful and powerful it can be!

No comments:

Post a Comment