Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract class and Access Modifiers

 
Gil Steidle
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand how the java access modifiers are working and have a scenario I need help with.
Is there a way to give access to the "name" variable only to the extending class (AbExtender) and not to the
instantiating class AbTester? Private will not allow the getName2 method in the sub-class access. Protected will, but it also lets the AbTester in as well.

Thanks!
Gil



 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there isn't. protected gives access to subclasses, but also to classes in the same package. Instead, what you'd usually do is use private and write the subclass as:

(Very early versions of Java did in fact have a modifier that did what you're asking, but it was dropped).

 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, you could have classes AbClass and AbExtender in one package (say, p1), and AbTester in another (say, p2). Thereafter, declare variable name with default (no) qualifier. The default scope is limited to classes in the same package.

Beware, though, variable name will then be visible to all the classes in package p1. Of course, you could opt to have no other class there.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, if you separate them in different packages as described in my earlier post, even 'protected' will achieve what you want.
 
Gil Steidle
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Taking your advice and splitting this into 2 packages, here is how I have it setup.
I've added the package and import statements to the classes but cannot get AbExtender to compile.
What am I missing???

Thanks!

C:\JAVA\SCJP\ch1\pkg1>javac AbExtender.java
AbExtender.java:3: cannot find symbol
symbol: class AbClass
class AbExtender extends AbClass
^
AbExtender.java:7: cannot find symbol
symbol : variable name
location: class pkg1.AbExtender
return "extended " + name;
^
2 errors

C:\JAVA\SCJP\ch1\pkg1>


Tree structure shows packages
c:
|__JAVA
.....|_____SCJP
...............|____ch1
........................|___pkg1
........................| AbClass
........................| AbExtender
........................|
........................|___pkg2
........................ AbTester


This file is in c:\JAVA\SCJP\ch1\pkg1\AbClass.java



This file is in c:\JAVA\SCJP\ch1\pkg1\AbExtender.java



This file is in c:\JAVA\SCJP\ch1\pkg2\AbTester.java


 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try something like this:


C:\JAVA\SCJP\ch1> mkdir bin

C:\JAVA\SCJP\ch1> javac -d bin pkg1\AbClass.java

C:\JAVA\SCJP\ch1> javac -d bin -cp . pkg1\AbExtender.java

C:\JAVA\SCJP\ch1> javac -d bin -cp . pkg2\AbTester.java



I didn't have time to execute it myself, but I've had such cases and the idea presented above works.
 
Gil Steidle
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, using the javac -d option did not work. It complained with:
C:\JAVA\SCJP\ch1>javac -d bin -cp . pkg1\AbExtender.java
javac: directory not found: bin
Usage: javac <options> <source files>
use -help for a list of possible options

C:\JAVA\SCJP\ch1>


 
Rob Spoor
Sheriff
Pie
Posts: 20667
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Create the bin folder first. And you'll want to use "-cp bin", not "-cp .". Your class files will be put inside the bin folder, and the package structure will be relative to it, not your current folder.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the erroneous solution. As I said, I hadn't executed it myself to test it out.

However, the idea I wanted to emphasize remains the same... we compile the independent classes to a particular output directory (say, bin), and then for compiling any dependent classes, we can include this output directory as classpath for javac.

Thanks @Rob.
 
Gil Steidle
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the 3 classes compile now but I can't get the darn thing to run. I've tried:

C:\>java c:\JAVA\SCJP\ch1\bin\pkg2\AbTester.class

Exception in thread "main" java.lang.NoClassDefFoundError: c:\JAVA\SCJP\ch1\bin\
pkg2\AbTester/class
Caused by: java.lang.ClassNotFoundException: c:\JAVA\SCJP\ch1\bin\pkg2\AbTester.
class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: c:\JAVA\SCJP\ch1\bin\pkg2\AbTester.class. Progra
m will exit.


And with a -cp

C:\>java -cp c:\JAVA\SCJP\ch1\bin c:\JAVA\SCJP\ch1\bin\pkg2\AbTester.class

Exception in thread "main" java.lang.NoClassDefFoundError: c:\JAVA\SCJP\ch1\bin\
pkg2\AbTester/class
Caused by: java.lang.ClassNotFoundException: c:\JAVA\SCJP\ch1\bin\pkg2\AbTester.
class
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: c:\JAVA\SCJP\ch1\bin\pkg2\AbTester.class. Progra
m will exit.

C:\>


Tree structure is now
c:
|__JAVA
.....|_____SCJP
...............|____ch1
........................|___bin
...............................|___pkg1
...............................| AbClass
...............................| AbExtender
...............................|
...............................|___pkg2
................................ AbTester

I've corrected the AbTester.java code to this:




 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The class name (having the main method) is represented in <package-name>.<class-name> fashion, your class name for 'java.exe' should be pkg2.AbTester, and not 'C:\JAVA\SCJP\ch1\bin\pkg2\AbTester'.

Try:

C:\>java -cp c:\JAVA\SCJP\ch1\bin pkg2.AbTester
 
Gil Steidle
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much! That works.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic