File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes access specifier protected Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "access specifier protected" Watch "access specifier protected" New topic
Author

access specifier protected

Saurabh Naik
Ranch Hand

Joined: Apr 24, 2008
Posts: 87
package pack1;
class Animal
{
protected void printMe()
{
System.out.println("in Animal's printme method");
}
}

package pack2;
import pack1.Animal;

class Horse extends Animal
{
public static void main(String args[])
{
Animal a = new Animal();
a.printme();
}
}

it gives me the error for protected access specifier.
why it is so ? we can access data from different package with protected by extending class.. ???


SCJP 6 (88%), SCWCD 5 (78%), preparing for SCBCD
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
horse inherits animal members. if you wish to access these members in horse then you do so by inheritance only i.e this.printMe() [or anotherHorseReference.printMe()]. you cannot access them using animal reference i.e they way you did.
[ August 01, 2008: Message edited by: Milan Sutaria ]

SCJP 6 83%
Working on SCWCD/OCPJWCD
Mike Mitchell
Ranch Hand

Joined: May 28, 2008
Posts: 38
You can do so, but it must be from a child reference. class Animal need also be public.



SCJP 5, SCWCD 5
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Originally posted by Mike Mitchell:
class Animal need also be public.


That's what came to my mind before seeing that the method is protected.


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: access specifier protected