aspose file tools*
The moose likes Beginning Java and the fly likes Abstract class and Access Modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract class and Access Modifiers" Watch "Abstract class and Access Modifiers" New topic
Author

Abstract class and Access Modifiers

Gil Steidle
Ranch Hand

Joined: Nov 19, 2010
Posts: 30
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

Joined: Apr 06, 2010
Posts: 4240
    
    7

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

Joined: Aug 25, 2003
Posts: 227

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

Joined: Aug 25, 2003
Posts: 227

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

Joined: Nov 19, 2010
Posts: 30
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

Joined: Aug 25, 2003
Posts: 227

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

Joined: Nov 19, 2010
Posts: 30
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

Joined: Oct 27, 2005
Posts: 19538
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

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

Joined: Nov 19, 2010
Posts: 30
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

Joined: Aug 25, 2003
Posts: 227

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

Joined: Nov 19, 2010
Posts: 30
Thank you so much! That works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Abstract class and Access Modifiers
 
Similar Threads
Overriding with Exception
trying to get a value from another class
abstract classes
Can We Access Child class members in super class in java ?
Override Inherited Constants?