This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hi I am having a doubt in protected access. I wrote the code as below. some thing is missing can anyone help on this.
I am having 2 pacakges, one defines protected variable in another one I am accessing in subclass. in this scenario it should not give any error. but it is thowing error
where it can be accessible without using reference to previous class since it is inheriting.
Thanks in advance
Sahid Khan
Ranch Hand
Joined: Jun 27, 2007
Posts: 41
posted
0
what is the error you getting? is the classpath set properly?
Pooja Jindal
Greenhorn
Joined: Aug 08, 2007
Posts: 10
posted
0
Well, If you have Protected variables/Method in class pack1class. and you are accessing it in subclass of pack1subclass (Please use Extends pack1class against it). Then, You can not access a Protected variable on Reference of the Super Class. So, pack1class pc=new pack1class(); System.out.println("protected method"+pc.prot); is Wrong!!
You should do it only through Inheritance. like,replacing above code piece with this System.out.println("protected method"+prot);
For More CClarifaication Refer to K& B Page no 35
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Originally posted by madhu v pe: Hi I am having a doubt in protected access.
Assuming that your classpath is correct, read the compiler message carefully. Your problem has nothing to do with protected access.
where it can be accessible without using reference to previous class since it is inheriting.
No, it's not inheriting. The reason that you don't get an error is that classes in the same package have the right to use members with default and protected access rights.
Manfred Klug
Ranch Hand
Joined: Jun 04, 2007
Posts: 377
posted
0
Originally posted by Pooja Jindal: pack1class pc=new pack1class(); System.out.println("protected method"+pc.prot); is Wrong!!
There is no problem with this code, since both classes are in the same package. Have a look at the table on page 39.
Himanshu Saxena
Greenhorn
Joined: Jul 18, 2007
Posts: 5
posted
0
Hi you are trying to use a non static field inside a static method. static method can access only the static fields. Learn about static ?
package pack2; import java.awt.*; import pack1.*; public class pack2subclassotherfile extends pack1class { public static void main(String[] sCommand) { System.out.println("protected method"+prot);// not a static field protMethod(); }
}
use this:
package pack2; import java.awt.*; import pack1.*;
public class pack2subclassotherfile extends pack1class{ String s=prot; //acess protected variable public static void main(String[] sCommand) { pack2subclassotherfile a=new pack2subclassotherfile(); System.out.println("protected method"+a.prot); a.protMethod(); }
}
madhu v pe
Ranch Hand
Joined: Apr 21, 2007
Posts: 100
posted
0
Hi all, Thanks for your replies,sorry I didn't mention the error. class path everything is ok.
Please read the topic Protected Details from K&B "Access Modifiers (objectives 1.3 and 1.4) there is an example
there is another package
In the same way I wrote my own code as below.
which produced correct output
protected methodprotected
I wrote another set of code to use protected instance variable and method as below
the output is non-static variable prot cannot be referenced from a static context System.out.println("protected method"+prot); non-static method protMethod() cannot be referenced from a static context protMethod();
what is fault in my code comarative to the book concept.
madhu v pe
Ranch Hand
Joined: Apr 21, 2007
Posts: 100
posted
0
Hi I have modified my second program as below
now it is working fine.
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.