Dhwani, sorry to be late on this. Check my comments to your comments. But first, have you tried running the code I posted? That is a good way to see how things work and don't work. Notice that all three dispx() methods use Test.a via inheritance. As long as subTest doesn't hide Test.a with it's own instance member "a" all three dispx() methods are accessing the same variable. All protected members of a superclass are visible to any subClasses whether or not the subClasses are in the same package or not.
in the first case you have used super keyword,
as per i know super is used to call constructor
of super class,how this possible
that we are accessing protected variables?
JK - In this case we are accessing super via inheritance. Super is always available to a subclass after the super constructor has been run. It is a special reference to the core essense/object of the subclass instance. Try setting super to a new instance of Test() and see what happens.
in the second case you have used this keyword..
As per i know this keyword refers
to current instance of class,as here current class is subTest
so instance variables of class subTest must be
called so how you can access protected members
of class Test?
JK - the current instance of subTest "inherits" any protected members of it's super class as if they were it's own members. There is no difference between it's own members and those it inherits from it's superclass. Unless of course subTest creates it's own member named "a". Try adding member int a = 15; to subTest. In this case subTest.a hides super.a.
Last case you have accessed directly protected variable
a.....how this is possible since both the
classes are in different packages atleast we need
a instance of subTest class to access variable a isnt it?
JK - Keep in mind that we are accessing and printing out the value of "a" within an instance of subTest via inheritance. The code is not accessing "a" from outside of subTest. Try creating another class in pack2 that tries to access mySubTestInstance.a.
After seeing these concepts of
protected access i am totally confused please
explain ......... [/QB]
JK - I sympathise with you. It seems like Sun overloaded the "protected" keywork. They should have created two access keywords. "packageprotected" and "inheritable".