Welcome to Dream.In.Code
Become a Java Expert!

Join 149,521 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,405 people online right now. Registration is fast and FREE... Join Now!




Access Protection Lesson Problems

 
Reply to this topicStart new topic

Access Protection Lesson Problems

itpro4470
6 Jul, 2007 - 04:27 PM
Post #1

D.I.C Head
Group Icon

Joined: 17 Jun, 2007
Posts: 123



Thanked: 1 times
Dream Kudos: 25
My Contributions
This is a lesson from the Complete Reference Java for J2SE 5 edition. It it supposed to teach about public, private and protected designations for variables and classes. It makes the following .java files in this order:
Package p1
Protection.java
Derived.java
SamePackage.java

Package p2
Protection2.java
OtherPackage.java

I am using Linux with the J2SE javac command line compiler. I am unsure of how to start compiling these files I have tried to converge all of the files into one large file and compile that it didn't work. I have tried to compile them in the order they were created and that doesn't work either. When trying the latter I am able to compile Protection but I am unable to compile Derived. I have included the code for both java files and the compiler errors I get. Thank You in advance for the help.

Protection.....Compiles fine
CODE

package p1;
public class Protection
{
    int n=1;
    private int n_pri=2;
    protected int n_pro=3;
    public int n_pub=4;
    public Protection()
    {
        System.out.println("base constructor");
        System.out.println("n= "+n);
        System.out.println("n_pri= "+n_pri);
        System.out.println("n_pro= "+n_pro);
        System.out.println("n_pub= "+n_pub);
    }
}


Derived doesn't compile
CODE

package p1;
class Derived extends Protection
{
    Derived()
    {
        System.out.println("Derived constructor");
        System.out.println("n= "+n);
        //Protection class only
        //System.out.println("n_pri= "+n_pri);
        System.out.println("n_pro= "+n_pro);
        System.out.println("n_pub= "+n_pub);
    }
}


here is the output from my compiler
CODE

eigrp@ospf:~/jDev/p1$ javac Derived.java
Derived.java:2: cannot find symbol
symbol: class Protection
class Derived extends Protection
                      ^
Derived.java:7: cannot find symbol
symbol  : variable n
location: class p1.Derived
                System.out.println("n= "+n);
                                         ^
Derived.java:10: cannot find symbol
symbol  : variable n_pro
location: class p1.Derived
                System.out.println("n_pro= "+n_pro);
                                             ^
Derived.java:11: cannot find symbol
symbol  : variable n_pub
location: class p1.Derived
                System.out.println("n_pub= "+n_pub);
                                             ^
4 errors

User is offlineProfile CardPM
+Quote Post

keems21
RE: Access Protection Lesson Problems
6 Jul, 2007 - 07:49 PM
Post #2

D.I.C Head
Group Icon

Joined: 3 Feb, 2007
Posts: 183



Thanked: 2 times
Dream Kudos: 25
My Contributions
The only suggestion that I have for you is to make sure that both class files are in the same folder. And yes, you need to compile the first program first because the second program extends it and that wouldn't be possible if the Protection class file didn't exist.

Let me know how everything works out.
User is offlineProfile CardPM
+Quote Post

itpro4470
RE: Access Protection Lesson Problems
6 Jul, 2007 - 08:14 PM
Post #3

D.I.C Head
Group Icon

Joined: 17 Jun, 2007
Posts: 123



Thanked: 1 times
Dream Kudos: 25
My Contributions
QUOTE(keems21 @ 6 Jul, 2007 - 08:49 PM) *

The only suggestion that I have for you is to make sure that both class files are in the same folder. And yes, you need to compile the first program first because the second program extends it and that wouldn't be possible if the Protection class file didn't exist.

Let me know how everything works out.

Thanks for the reply!
They are all in their respective folders. It seems as if Derived is not seeing the variables in Protected even though it extends it.

This post has been edited by itpro4470: 6 Jul, 2007 - 08:15 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Access Protection Lesson Problems
7 Jul, 2007 - 01:31 AM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi it doesn't compile because you don't call the variables from the Protection class. All you have to do is before the names n, n_pri, n_pro, n_pub just add the word super. don't forget the dot.
That is how you access the base class members whether they are functions or variables.
So the code would be:

CODE
package p1;
class Derived extends Protection
{
    Derived()
    {
        System.out.println("Derived constructor");
        System.out.println("n= "+super.n);
        //Protection class only
        //System.out.println("n_pri= "+super.n_pri);
        System.out.println("n_pro= "+super.n_pro);
        System.out.println("n_pub= "+super.n_pub);
    }
}

For more reading check HERE for the super keyword and HERE for the this keyword.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Access Protection Lesson Problems
7 Jul, 2007 - 02:20 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
QUOTE(PennyBoki @ 7 Jul, 2007 - 11:31 AM) *

Hi it doesn't compile because you don't call the variables from the Protection class. All you have to do is before the names n, n_pri, n_pro, n_pub just add the word super. don't forget the dot.
That is how you access the base class members whether they are functions or variables.


!!!!!!!Not true!!!!!!!!!
Adding super in this situation is not necessary.
You only have to use super if the variable of the base class is hidden in the current scope (that is not true in this case), or when you call constructors, because they are not inherited.


itpro:
For me your code compiles just fine, and runs ok.
Check the following:
1) are those java files (Protection.java and Derived.java) both in a directory called p1?
2) cd to the parent directory of p1
3) write javac p1/*.java (this way the compiler will take care of dependency problems, and you don't have ot care about what ot compile first...)
If it works, and doesn't produce any error, then you might have a problem with jDev's or the project's configuration.

This post has been edited by 1lacca: 7 Jul, 2007 - 02:21 AM
User is offlineProfile CardPM
+Quote Post

ap0c0lyps3
RE: Access Protection Lesson Problems
7 Jul, 2007 - 03:11 AM
Post #6

D.I.C Head
Group Icon

Joined: 19 Jun, 2007
Posts: 79


Dream Kudos: 25
My Contributions
The class files also have to be in the same folder when you execute
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Access Protection Lesson Problems
7 Jul, 2007 - 04:26 AM
Post #7

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
I was wrong thanks for correcting me.

User is offlineProfile CardPM
+Quote Post

itpro4470
RE: Access Protection Lesson Problems
7 Jul, 2007 - 07:14 AM
Post #8

D.I.C Head
Group Icon

Joined: 17 Jun, 2007
Posts: 123



Thanked: 1 times
Dream Kudos: 25
My Contributions
Hmm I may have to look into getting a new compiler then...I tried it with super.n etc that yields the same thing and I have tried going to the parent directory and typing javac p1/*.java which actually gets past derived but get stuck with the same exact messages on another .java called SamePackage which makes the same calls to all but private but it can't even see the public variable. Does anyone else have Linux (Debian based)with a good compiler? if so let me know where I can get one. Thanks!!!
User is offlineProfile CardPM
+Quote Post

itpro4470
RE: Access Protection Lesson Problems
7 Jul, 2007 - 08:20 AM
Post #9

D.I.C Head
Group Icon

Joined: 17 Jun, 2007
Posts: 123



Thanked: 1 times
Dream Kudos: 25
My Contributions
I just got NetBeans 5.5.1 it's an awesome IDE but I still have get used to it, seems like is was designed for very large projects, it's nothing like the good ol' command line compiler and I have to use it on my Windows Partition but it ran this program fine. I guess i'm not 1337 enough to program in Linux yet I'll learn one of these days.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Access Protection Lesson Problems
7 Jul, 2007 - 02:41 PM
Post #10

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
If NetBeans works fine, then go with it right now. However I'm sure that it's not a jDev problem, but some installation or configuration (editor or project, or simply a misplaced source file).


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month