• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access Type - Protected

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I don't understand the raison d'etre of "Protected"..

I have a following case -

This code does not execute until I keep Trade class in the same package Food even if I import the Apple class;

I have two doubts here - how does changing the packge help here?

secondly , protected makes a member "inheritable". And once it has been inherited - should be accessible from amy package as long as long as object is creatable.

if there is any error in my code - please correct me.
[ September 12, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public means to give all classes from all packages access to your class method

private means to give access to only the class in which it was declared

default (no modifier) gives access to classes in the same package

protected means to give access to classes in the same package and by any subclass- including outside the package

In your example, you do not have inheritence. You need to use the keyword "extends" to have Apple derive from Fruit.
 
sharad narang
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh that was something I missed while typing.. my intention was to subclass Apple from Fruit. my question still remains.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
one word CLASSPATH. when you declare the package you have to specify it in the classpath. I tried your code out just to be sure and it works like it should with Trade in a different package(yes I did the extension) (in this case in C:\javastuff without a package declaration).
I have food under C:\javastuff\food
and I set my class path to
│set PATH=%PATH%;C:\j2sdk1.4.2_05\bin;.
│set CLASSPATH=C:\j2sdk1.4.2_05\lib;c:\javastuff;.

you can also st it in the commmand line as an option.
Remember its never the language its always you
[ September 13, 2004: Message edited by: Benjamin Gunawardana ]
 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ... An extension to this Thread ... Just thought would summarize the two important steps that are to be followed when creating packages! So that, I make it clear to those people, who find it hard with packages and also to clear myself

Say I have the package "food" created under "c:\java", meaning my Fruit.java, Apple.java, Fruit.class, Apple.class are under the folder c:\java\food.

I have the file Trade.java in the folder c:\java. Now, the two important steps are:

1. APPEND to your CLASSPATH, c:\java
2. use "import food.*;" in your Trade.java

When Trade.java is compiled, compiler comes to the "import food.*;" statement. It then takes each and every path from CLASSPATH one by one, and appends "food" to it and checks, whether it is a valid directory. If yes, then looks for classes Apple and Fruit inside it. If right files are found, it gives no compilation error.

Hope I made it clear! Thanks!
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are using "package food;" in your Fruit.java and Apple.java right?
 
Manikandan Jayaraman
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yaa... Very much!

Only then, they belong to the "food" package and will allow me to import in Trade.java ... The correction to the original code is, remove the "package food;" statement of Trade.java and then keep this file in any directory, required.

Following the other steps which I wrote in my previous post, will now make Trade.java to use Fruit and Apple. Hope I am making sense!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic