Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Java > Java and JDK > BOOK: Beginning Java 2
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Java 2
This is the forum to discuss the Wrox book Beginning Java 2, SDK 1.4 Edition by Ivor Horton; ISBN: 9780764543654
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Java 2 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old March 16th, 2004, 10:44 PM
Registered User
 
Join Date: Mar 2004
Location: Montreal, , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Protected access

Hello!
The access modifier protected permits access to classes in the same package and subclasses anywhere right?
But when I define a subclass in a different package than the super class, I cannot access variables that are declared to be protected in the super class. can you explain that?
Thank you!
  #2 (permalink)  
Old March 17th, 2004, 09:14 AM
Registered User
 
Join Date: Dec 2003
Location: Prague, , Czech Republic.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

It should working.
Have You set the super class for public?
For example:
the definition of the parent(super) class:

package first;

public class First {
  protected String protectedProperty = null;
  protected void protectedFunction() {
    System.out.println("From protected function.");
  }
  public First() {
    protectedProperty = new String("It is working.");
  }
}

the definition of the child class (subclass):
package second;

import first.First;

class Second extends First {
  public static void main(String args[]) {
    Second s = new Second();
    s.protectedFunction();
    System.out.println(s.protectedProperty);
  }
}

It is working...
Have a nice day!

Gabriel :o)


  #3 (permalink)  
Old March 17th, 2004, 11:17 AM
Registered User
 
Join Date: Mar 2004
Location: Montreal, , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for the time and trouble you took to answer my question!
In your example s is of type Second, and you are accessing s fron Second which ,in my view, should work. The problenm is if you try to access the memebers protectedProperty or protectedFunction of an element of type First from class Second.

Thank you again and have a nice day!

wourouds
  #4 (permalink)  
Old March 17th, 2004, 11:33 AM
Registered User
 
Join Date: Dec 2003
Location: Prague, , Czech Republic.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

If You create an instance of First class in package second, than You can not access any members of First class except of public ones. (Because You are in a different package).
It is a correct behavior.

Best regards,

Gabriel

  #5 (permalink)  
Old March 18th, 2004, 03:27 AM
Registered User
 
Join Date: Dec 2003
Location: Prague, , Czech Republic.
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello again :o)

Here is a final explanation for this problem:
Consider the following example:

super class:
package a;

public class First {
  protected String protectedProperty = null;
  protected void protectedFunction() {
    System.out.println("From protected function.");
  }
  public First() {
    protectedProperty = new String("It is working.");
  }
}

subclass:

package b;

import a.First;

public class Second extends First {
  public static void main(String args[]) {
    First f = new First();
    Second s = new Second();
    // f.protectedFunction(); - compilation error
    s.protectedFunction();
    // System.out.println(f.protectedProperty); - compilation error
    System.out.println(s.protectedProperty);
  }
}

The commented lines in the class definition of Second are compilation errors because You are in package b. As You know, there are at least four access modifiers in Java: public, private, protected and 'package access'. In package b You can access only public fields of classes belonging to package a. However You can access protected fields also, but only if You inherit a subclass in package b => then You can access protected fields, BUT ONLY IF YOU ACCESS THESE FIELDS AS THE FIELDS OF THE SUBCLASS. You can not access them through the object f (from the example), as in this case You have tried to access them through a different object, then the subclass itself.

Have a nice day!

Gabriel :o)


 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Crystal Report 9 password protected mdb ACCESS sajid_rauf Pro VB Databases 2 July 27th, 2007 03:29 AM
sub report in a password protected access database aasheesh_jha Crystal Reports 0 January 2nd, 2007 07:09 PM
disadvantages of protected access specifier kishore.dyn Java Basics 2 August 10th, 2006 04:31 PM
Protected Area Demo ASP.NET 2.0 Basics 0 May 25th, 2006 01:12 PM
Access and embedding Password Protected Documents dkb Access 4 January 16th, 2004 09:11 AM



All times are GMT -4. The time now is 04:16 PM.


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.