|
-
Sep 26th, 2004, 01:33 PM
#1
Thread Starter
Hyperactive Member
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
-
Sep 26th, 2004, 04:17 PM
#2
Dazed Member
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 );
}
}
Last edited by Dilenger4; Sep 26th, 2004 at 04:29 PM.
-
Sep 26th, 2004, 04:59 PM
#3
Thread Starter
Hyperactive Member
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
-
Sep 26th, 2004, 06:03 PM
#4
Dazed Member
Posted by voidflux
Thanks for the reply, but the problem is, they are in different packages.
Yes i know that.
-
Sep 26th, 2004, 06:51 PM
#5
Thread Starter
Hyperactive Member
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
-
Sep 28th, 2004, 06:50 AM
#6
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.
-
Sep 28th, 2004, 09:42 AM
#7
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|