• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

access specifier protected

 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.. ???
 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do so, but it must be from a child reference. class Animal need also be public.

 
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Remember to always leap before you look. But always take the time to smell the tiny ads:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic