Hi,I am preparing for sjcp exam.my quetion is about the protected class. it have one super and sub class example ,below example i read from one pdf.package certification;public class Parent { protected int x = 9; // protected access}----------------------------package other;import certification.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 the p reference? System.out.println("X in parent is " + p.x); // Compiler error!}}when i compile this code it dosen't give any error.even though both classes are in diffrent package. but in pdf it mentioned that it gives an error because you are trying to access the protected variableusing the instance of the super class if the above code is write then what is logic behind the protected member of super class and if sub classin different package try to access the member of the super class.

Questions by Thakkar Nirav   answers by Thakkar Nirav

Showing Answers 1 - 10 of 10 Answers

john

  • May 27th, 2006
 

HI,Just checked, i get compiler Error when i try in different packages. // as your Pdf says.I am using jdk141_03 .But why does the error occur ?? Explain please...

  Was this answer useful?  Yes

Moorthi

  • May 29th, 2006
 

I am also getting compilation error.but we can access the protected variable if it is in different package.The parent class protected variable will be a private variable in the sub class.The code should be,System.out.println("X in parent is " + x);instead ofSystem.out.println("X in parent is " + p.x);

  Was this answer useful?  Yes

Moorthi

  • May 29th, 2006
 

Using the Object we can access only the public variable.if any contradiction in the above statement please reply with suitable exampleThanks Moorthi M

  Was this answer useful?  Yes

Hi,I am also getting the compilation error. As per my knowledge we need not create a Object for call the Super class "Protected" variable. An Object is mainly used for access the public variables or methods of a class.The above code will run when the code shoule be,System.out.println("X in parent is " + x);instead ofSystem.out.println("X in parent is " + p.x); // Compiler error!if u have any condratiction in my statements please reply with suitable example then that will help more.chearsMoorthi M

  Was this answer useful?  Yes

gururaj

  • Jun 20th, 2006
 

hi friends,

     In java protected variable/method can be accesed by subclass provided the parent and child are both in the same package, if the child is in different package, we cannot access the protected variable using the form obj.var/ obj.method...

Guys who are still skeptic about my answer please go to this site and confirm for urself. Note the 3rd column in the table says SubClass access outside package in a way it is misleading... the 3rd column means that for simply accessing the variable by name  --System.out.println("x is " + x); -- then JVM traces it back to parent class and is legal but if u access it using the type obj.var or obj.method it gives compilation method since only public methods were designed for this sort of access.

http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html

  Was this answer useful?  Yes

nikita sanyal

  • Jun 21st, 2006
 

I think dot operator can be used only with public .So change the protected to public and it will run fine

  Was this answer useful?  Yes

samidha

  • Aug 17th, 2006
 

  a protected member is accessible in all class in package containing its class,& by all subclass of its class in any package where this class is visible.

  a subclass in another package can only access protected members in the superclass via refences of its own type or its subtype.

If ur preparing for sjcp 1.4 then refer book- java certification by khalid Mughal. It is good.

  Was this answer useful?  Yes

sandhya

  • Apr 16th, 2007
 



      if i am not wrong we can use the super keyword to access the member variable of different classes and we need not create an object i think the compiler error is du e to the object creation and accessing the variable through the object.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions