Wednesday, September 5, 2012
Today I am going to post how the access-specifier protected is related with inheritance in Java. It is less restricted than default but restricted than public.
How protected works and is different from default:Default and protected behavior differ only when we talk about subclasses.If the
protected keyword is used to define a member,any subclass of the class declaring the member can access it through inheritance.It doesn't matter if the superclass and subclass are in different packages,the protected superclass member is still visible to the subclass in a very specific way.This is in contrast to the default behavior,which doesn't allow a subclass to access a superclass member unless the subclass is in the same package as the superclass.
Whereas default access doesn't extend any special consideration to subclasses (you're either in the package or you're not), the protected modifier respects the parent-child relationship, even when the child class moves away (and joins a new package). So, when you think of default access, think package restriction. No exceptions. A class with a protected member is marking that member as having package-level access for all classes, but with a special exception for subclasses outside the package. But what does it mean for a subclass-outside-the-package to have access to a superclass (parent) member? It means the subclass inherits the member. It does not, however, mean the subclass-outside-the-package can access the member using a reference to an instance of the superclass. In other words, protected = inheritance. Protected does not mean that the subclass can treat the protected superclass member as though it were public. So if the subclass-outside-the-package gets a reference to the superclass (by, for example, creating an instance of the superclass somewhere in the subclass' code), the subclass cannot use the dot operator on the superclass reference to access the protected member. To a subclass-outside-the-package, a protected member might as well be default (or even private), when the subclass is using a reference to the superclass. The subclass can see the protected member only through inheritance.
package package1;
public class Parent {
protected int x = 20; // protected access
}
package package2;
import package1.Parent;
class Child extends Parent {
public void testIt() {
System.out.println("x is " + x); // No problem;Child inherits x
Parent p = new Parent(); // Can we access x using p reference?
System.out.println("X in parent is " + p.x); // Compiler error!
}
}
The last line will give an error as following
package2/Child.java: x has protected access in package1.Parent
System.out.println("X in parent is " + p.x);
1 error
Hope this helps. Keep looking for more posts.
How protected works and is different from default:Default and protected behavior differ only when we talk about subclasses.If the
protected keyword is used to define a member,any subclass of the class declaring the member can access it through inheritance.It doesn't matter if the superclass and subclass are in different packages,the protected superclass member is still visible to the subclass in a very specific way.This is in contrast to the default behavior,which doesn't allow a subclass to access a superclass member unless the subclass is in the same package as the superclass.
Whereas default access doesn't extend any special consideration to subclasses (you're either in the package or you're not), the protected modifier respects the parent-child relationship, even when the child class moves away (and joins a new package). So, when you think of default access, think package restriction. No exceptions. A class with a protected member is marking that member as having package-level access for all classes, but with a special exception for subclasses outside the package. But what does it mean for a subclass-outside-the-package to have access to a superclass (parent) member? It means the subclass inherits the member. It does not, however, mean the subclass-outside-the-package can access the member using a reference to an instance of the superclass. In other words, protected = inheritance. Protected does not mean that the subclass can treat the protected superclass member as though it were public. So if the subclass-outside-the-package gets a reference to the superclass (by, for example, creating an instance of the superclass somewhere in the subclass' code), the subclass cannot use the dot operator on the superclass reference to access the protected member. To a subclass-outside-the-package, a protected member might as well be default (or even private), when the subclass is using a reference to the superclass. The subclass can see the protected member only through inheritance.
Showing accessibility of protected |
The code below will explain it further
package package1;
public class Parent {
protected int x = 20; // protected access
}
package package2;
import package1.Parent;
class Child extends Parent {
public void testIt() {
System.out.println("x is " + x); // No problem;Child inherits x
Parent p = new Parent(); // Can we access x using p reference?
System.out.println("X in parent is " + p.x); // Compiler error!
}
}
The last line will give an error as following
package2/Child.java: x has protected access in package1.Parent
System.out.println("X in parent is " + p.x);
1 error
Hope this helps. Keep looking for more posts.
Labels:Inheritance
Subscribe to:
Post Comments
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...
To my mind that's what I call a good blog article! Do you utilize this website for private goals solely or you have it to get profit with its help?
ReplyDeleteI am just using this blog to help other people nad encourage them about Java
Delete