File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Cannot acces Protected Variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Cannot acces Protected Variable" Watch "Cannot acces Protected Variable" New topic
Author

Cannot acces Protected Variable

Kasun Wixkramanayake
Ranch Hand

Joined: Feb 09, 2010
Posts: 74



when i try this programme i got some compile error,why i cannot access protectedattribute using ref object variable
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4240
    
    7

Subclasses can access protected members by inheritance. So an instance of the subclass can access it's own protected member from the superclass, but it can't access the same member on a different object. Your example neatly demonstrates exactly what this means - line 27 compiles but line 24 doesn't.
Sathiesh Kumar Vs
Ranch Hand

Joined: Oct 24, 2010
Posts: 31
Hi

As Mathew said, you are trying to access the protected variable on the reference of the Super class, which you cannot do.
This is as well same as OtherSubClassA not extending ClassA.


Thanks and Regards,
Sathiesh.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot acces Protected Variable
 
Similar Threads
Accessing protected inner class with .new
New Doubt: from SCJP6.0 K&B MockExam-1
ArrayList confusion
Accessing class variables from another class
protected