9 Replies - 1947 Views - Last Post: 07 July 2007 - 03:41 PM Rate Topic: -----

#1 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Access Protection Lesson Problems

Posted 06 July 2007 - 05:27 PM

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
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
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
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



Is This A Good Question/Topic? 0
  • +

Replies To: Access Protection Lesson Problems

#2 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Access Protection Lesson Problems

Posted 06 July 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.
Was This Post Helpful? 0
  • +
  • -

#3 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: Access Protection Lesson Problems

Posted 06 July 2007 - 09:14 PM

View Postkeems21, on 6 Jul, 2007 - 08:49 PM, said:

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: 06 July 2007 - 09:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Access Protection Lesson Problems

Posted 07 July 2007 - 02: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.
So the code would be:

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.
Was This Post Helpful? 0
  • +
  • -

#5 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Access Protection Lesson Problems

Posted 07 July 2007 - 03:20 AM

View PostPennyBoki, on 7 Jul, 2007 - 11:31 AM, said:

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: 07 July 2007 - 03:21 AM

Was This Post Helpful? 0
  • +
  • -

#6 ap0c0lyps3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 19-June 07

Re: Access Protection Lesson Problems

Posted 07 July 2007 - 04:11 AM

The class files also have to be in the same folder when you execute
Was This Post Helpful? 0
  • +
  • -

#7 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Access Protection Lesson Problems

Posted 07 July 2007 - 05:26 AM

I was wrong thanks for correcting me.
Was This Post Helpful? 0
  • +
  • -

#8 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: Access Protection Lesson Problems

Posted 07 July 2007 - 08:14 AM

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!!!
Was This Post Helpful? 0
  • +
  • -

#9 itpro4470  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 156
  • Joined: 17-June 07

Re: Access Protection Lesson Problems

Posted 07 July 2007 - 09:20 AM

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.
Was This Post Helpful? 0
  • +
  • -

#10 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Access Protection Lesson Problems

Posted 07 July 2007 - 03:41 PM

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).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1