Results 1 to 7 of 7

Thread: Problem accessing an object in a different package...

  1. #1

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290

    Resolved Problem accessing an object in a different package...

    Hello everyone, i'm having problems accessing an Intger Object orginally created in MIS class. I'm suppose to, "3.Implement the CE class so it can print out the value mis_y originally stored in the MIS class. "

    This is the structure of all my classes i've created.

    I have two packages,
    Gannon and Behrend
    Inside the Gannon package I have the following classes:
    CIS
    MIS
    I also have an inferface called Department;
    CIS and MIS both implement the Department inferface
    Inside the Behrend package I have the following classes:
    SE
    CE
    EE
    I also have an interface called School;
    SE,CE, and EE all implement the School interface.
    CE also extends the MIS class so I can access a protected data member, mis_y;
    mis_y is protected in the MIS class.
    Code:
    package Gannon;
    public class MIS implements Department {
      public MIS() {
      }
    
     Integer mis_x = new Integer(300);
     protected Integer mis_y = new Integer(500);
     private Integer mis_z;

    Here is my code to the CE class:
    Code:
    package Behrend;
    import Gannon.MIS;
    
    public class CE extends MIS implements School  {
      public CE() {
      }
      
     
      
      public static void main(String args[])
      {
        MIS b = new MIS();
        
        System.out.println("mis_y: "+ b.mis_y );
      }

    When i run this file, i get the following error:
    "CE.java": mis_y has protected access in Gannon.MIS at line 24, column 37

    Which makes no sense because protected data members are accessible to a different package subclass.

    Any idea's what i did wrong?

    Thanks!
    Last edited by Dilenger4; Oct 7th, 2004 at 05:09 PM.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    When i compile and run your code with both classes in the same file everything runs fine. But in this case it really doesnt matter if mis_y is defined as being protected or with no access modifer(default package access). Since both classes reside in the same package. misy_y will be able to be access by all classes within the same package even non sub classes of MIS.
    Code:
    class MIS{
      public MIS() {}
    
      Integer mis_x = new Integer(300);
      protected Integer mis_y = new Integer(500);
      private Integer mis_z;
     }
    
     public class CE extends MIS {
      public CE() {
     }
      
     
      public static void main(String args[]){
         MIS b = new MIS();
         System.out.println("mis_y: "+ b.mis_y );
      }
     }

  3. #3

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    Thanks for the reply, but the problem is, they are in different packages. If you look at my above source, MIS class is in the Gannon Package, while CE is in the Behrend package.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  4. #4

  5. #5

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    So is there a solution to my problem, if the 2 classes are in different packages? and I want CE to access MIS's data member which is protected?
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I believe the problem is that the static method of CE might not have access to the protected member of MIS. Try putting the access into a non-static method of CE and see if that works.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Hyperactive Member voidflux's Avatar
    Join Date
    Jun 2003
    Location
    Brockway, PA
    Posts
    290
    Thanks CornedBee, you always seem to know the problem! My instructor told us that exact solution at around 8:00am today! Thanks! It works great.
    C¤ry Sanchez
    Computer Science/Engineering
    @ Penn State
    IBM.zSeries Intern
    Mandriva 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width