Please join us at the new JavaWorld Q&A Forums. Your existing login will work there. The discussions here are now read-only.


JavaWorld >> Java Beginner

Pages: 1
saurabh_naik
stranger


Reged: 08/02/07
Posts: 14
access specifier protected
      #177164 - 08/01/08 05:38 PM

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.. ???


Post Extras: Print Post   Remind Me!   Notify Moderator  
hiwa
Carpal Tunnel


Reged: 06/21/03
Posts: 7704
Loc: Japan
Re: access specifier protected [Re: saurabh_naik]
      #177212 - 08/01/08 05:36 PM

Your code has lots of syntax error and your intended package structure is unclear. Post a small demo code that is generally compilable, runnable and could reproduce your problem. See: http://homepage1.nifty.com/algafield/sscce.html and http://www.yoda.arachsys.com/java/newsgroups.html

--------------------
*stop cruelty* Annual number of euthanized cats&dogs: US 5M, JP 500K.*for our better karma*


Post Extras: Print Post   Remind Me!   Notify Moderator  
hiwa
Carpal Tunnel


Reged: 06/21/03
Posts: 7704
Loc: Japan
Re: access specifier protected [Re: hiwa]
      #177279 - 08/01/08 09:53 PM

Due to the malfunctioning of the forum software, you are redirected to see the text in: http://homepage1.nifty.com/algafield/answer.txt

--------------------
*stop cruelty* Annual number of euthanized cats&dogs: US 5M, JP 500K.*for our better karma*


Post Extras: Print Post   Remind Me!   Notify Moderator  
gauravrajbehl
stranger


Reged: 08/02/08
Posts: 1
Re: access specifier protected [Re: hiwa]
      #177529 - 08/03/08 01:52 AM

Hi Saurabh,

The syntax seems to be correct, but there are couple of problems with the code which violates the java1 principles:

1) Both the classes have different access levels i.e. default and also they are in different packages. Default access classes are invisible to each other if they have default access. In order for a class to extend another class, the parent class must be either in the same package or the parent class must be public.

2) Another problem is with the protected access specifier, protected specifier applies to inheritance i.e. child classes and anything outside the package i.e if the child class is in different package and the parent class having the protected method is in the different package, the child class cannot instantiate a parent class object as it will be invisible to the child class (just like private method) but yes, the parent class protected method will be available to the child class through inheritance, so instead of creating a new object of the parent class (which is invalid), in child class from main, call a method and access your printMe() method from there. So, two things you must do for this code to work:

1) Make your parent class public i.e. public Animal

2) Call another method from your main in child class and from that child class you can access the printMe method.

I hope this helps
Thanks.
Gaurav Raj Behl


Post Extras: Print Post   Remind Me!   Notify Moderator  
Pages: 1



Extra information
0 registered and 3 anonymous users are browsing this forum.

Moderator:   

Print Topic

Forum Permissions
      You cannot start new topics
      You cannot reply to topics
      HTML is disabled
      UBBCode is enabled

Rating:
Topic views: 5917

Rate this topic

Jump to

Contact us JavaWorld

Powered by UBB.threads™ 6.5.5