Above are three classes. Please pay attention to class packages
i want to know that in Class Test3 why i am being able to access the test2.var1 variable. As once Class Test3 have inherited the var1 variable from Test1 Class
it should be like we have protected variable in Test3 Class. So why we are being able to access it from other package with a reference variable...(Test2) ?
Waheed Murad wrote:So why we are being able to access it from other package with a reference variable...(Test2) ?
What "other package"? Remember that variables aren't inherited (so the title of your post is misleading). The variable is declared in package "general.pack1" and the code which attempts to use the variable is declared in that same package.
You could experiment with declaring your Test3 class to be in package general.package2 or in some completely different package.
Waheed Murad wrote:Ok Class Test2 inheriting it from Test1 Class. But as i am using the object to refer it so shouldn't it be treat like it is declared in Class Test2. in which case it should give exception.
Well, the compiler disagrees with you. So are you right or is the compiler right? I know which the two I would believe...