jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Here is some food for thought in Java :-) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Here is some food for thought in Java :-)" Watch "Here is some food for thought in Java :-)" New topic
Author

Here is some food for thought in Java :-)

maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hello all,
This qstn is not from any mock/real exam. Just for giving you to verify your answers. Feel free to try it
regds
maha anna
The foll. 2 prog. are in 2 separate files. What happens when you compile with javac -d . Point.java and javac -d . PlusPoint.java ? Is there any compile error? If not what's happens in the PointPlus.java. How can it have a method move(..)with lesser access than that of the super class's method.
regds
maha anna
<pre>

package points;
public class Point {
public int x, y;
void move(int dx, int dy) { x += dx; y += dy; }
public void moveAlso(int dx, int dy) { move(dx, dy); }
}
package morepoints;
public class PlusPoint extends points.Point {
private void move(int dx, int dy) {
moveAlso(dx, dy);
}
}

</pre>

[This message has been edited by maha anna (edited April 18, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
In the first case the access modifier is default.. so that method is accessible to the classes in the same package.. so in the package morepoints actual method of the superclass is not visible.. and so it can have the method move(int x, int y )which has lesser access than the original method. Am I right?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
My vote for Vinni
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Yes. You are correct vinni. There is something more intersting to discuss for this code. I wait for some more people to come in.
regds
maha anna
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
After correction of your message (javac -d . PlusPoint.java NOT PointPlus.java) the interesting thing that comes up is:
the default access for move() in Point class will prevent you from overriding this method in PlusPoint class. Making move() as private (with lesser access) in Point class will allow you to override this method in PlusPoint class. In short: the default access has wider access (package conserning) than private, but more restrictive for overriding than private access (in case when subclass is located in different package).
Ihor Strutynskyj
Carol Willing
Greenhorn

Joined: Apr 10, 2000
Posts: 2
Maha,
I tried the examples. When compiling PlusPoint, the compiler complained that move could not be overridden. I believe that the reason for this is that a subclass defined in a different package does not inherit private or default methods.
I could get PlusPoint to compile if move in Point was changed to private. In this case, is move really being overridden or is something else going on?
Any hints?
Carol
SaiRam NageshKumar
Greenhorn

Joined: Apr 06, 2000
Posts: 21
In the class Point, move method is of type friendly(as per RH book) so it is accessed by the classes of the package Points only. And the method moveAlso is declared public so it is generic and can be accessed by any class of any package.
Coming to the second class, PlusPoint class of package morePoints, this class tried to override the method moveAlso but this is not possible since public method cannot be made private(but reverse is possible acc to RH) and one more thing from this class under this moveAlso method tried to access the move method of the parent class this is not possible(due to the cause mentioned in first paragraph).
I hope I am in adequate explanation! If anything wrong let me know.
Thanks!
Sai Ram.
Sanath Kumar
Greenhorn

Joined: Apr 13, 2000
Posts: 5
Hi maha anna,
Because the method in point is accessable with in the package and it can not be overridden by move in PlusPoint which is there in another package.
If we remove super in PlusPoint then it will work fine Because the original move method is not accessible from package morepoints.
as per JLS
If a public class has a method or constructor with default access, then this method or constructor is not accessible to or inherited by a subclass declared outside this package.
Hope this will add some light to the discussion.
-sanath

[This message has been edited by Sanath Kumar (edited April 17, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Carol- It sounds like something is wrong with your setup. Are you using two separate files, Point.java and PlusPoint.java? Are the two package statements different? There should be no error message, because move() in points.Point does not even exist as far as package morepoints is concerned. It should be effectively invisible to the compiler. Look carefully at the error message you get - it should identify the exact classes an packages the compiler thinks are involved, which should be points.Point and morepoints.PlusPoint. Are the two classes indeed being recognized as being in separate packages? If not, that would explain the error message.
If you can't find the problem based on the above suggestions, please tell us the exact text of the error message. Also, what compiler are you using? And what OS? Thanks.


"I'm not back." - Bill Harding, Twister
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Point.java compiles fine, it has nothing to do with PlusPoint.java, As vinni said move() has default access, hence only accessible to all classes in the Points package, is not accessible to subclass in the different package. You can't override a default access move() to be more restrictive(private) if it all exists in the same package(points package). PointPlus.java does not compile.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
VCS- it's not supposed to all be in the same package. PlusPoint is in the package morepoints, not points. If you set the packages correctly, it should compile. If it doesn't, please tell us the exact error message you're getting.
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Hello all again,
In order to run the program we have to first compile it successfully right? This is the setup needed to compile the above 2 file. I had already told what to do. I repeat it again.
1.You can test this concept in any directory.
2. You have to crate 2 files namely (case sensitive) Point.java and PlusPoint.java
3. cut and paste the above program including the package statement in their correspoining files. You all must have known this because in Java in 1 single source file there can be maximum 1 public class file and the file MUST BE named after the public class which is inside the source file.
4. Now you have 2 .java files Point.java and PlusPoint.java in your current directory with all the correct code inside them
5. Compile javac -d . Point.java
6. Compile javac -d . PlusPoint.java
7. There SHOULD NOT be any problem in compilation because the -d . option creates the needed sub directories for the compilation unit.
8. Now after compilation there would have been 2 sub-directories created under your current dir where you did the compilation. They are points and morepoints. Inside the points dir, the Point.class would have been created and simillarly inside morepoints directory PlusPoint.class would have been created.
So now you have the simillar dir structure.
<pre>

c:\test\Point.java
c:\test\PlusPoint.java
c:\test\points\Point.class
c:\test\morepoints\PlusPoint.class
</pre>
--------------------------------------------------------------
Coming back to our original discussion, here
1. we have 2 completely different packages points and morepoints.
2. The Point.class belongs to ONLY points package
3. The PlusPoint.class belongs to ONLY morepoints package
4. The Point class has a method called move(...) which has the default or in other word the package level access.Which means this class Point is well known to all the other classes inside the same package 'points'. Also note that though the class Point is available to the whole world , still the method move(..) inside Point.java says, 'I have my own wishes. It is me who takes the decision. If you as a class can be public. I don't bother' like that.
5. Since Point.class gives free access to all by means of public access keyword, eventhough it is kept inside 'points' package, it CAN BE extended or used in other packages also.
6. This public access of Point.class gives freedom to the other unrelated class 'PlusPoint.class' in unrelated package 'morepoints' to extend it.
7. The important point here to note is as I said before, eventhough points.Point.class is public points.Point.move(..) method is not granted here . So the morepoints.PlusPoint.class decides to write on its own version of move(...) method. When can you override a method? If it is inherited from the super class only right? When the super class version is NOT EVEN KNOWN to the subclass (since superclass move belongs to its own pack ), how can it override? If the programmer decides to separate the 2 files in different packages then he has to overcome the consequences.
8. So the PlusPonint's move method is its own version of fresh-born new method. It can have any type of access level . (private/protected/public). There is NO-CONNECTION bet the super class version and the subclass version here.
9. Also note that the 'moveAlso(..)' method is granted to PlusPoint.class because it has public access level. So when u call movealso(..) from PlusPoint.class, it is executed, and you know what? .. it, calls the parent version of the move which is in Point.class NOT IN PlusPoint.class. Because the origianl inherited version of movealso called the move(..) version which is inside its package and since it is not overridden here in PlusPoint.class the super version is called.
10. ALso if you would have in fact changed the access level of the Point class move to public and if you would have overridden now in PlusPoint.class , and when u call moveAlso(..) , now the overridden version would have been called , which leads to moveAlso(..) calls move(...) which again calls moveAlso(...) which again calls move... leads to StackOverflow at run time
regds
maha anna

[This message has been edited by maha anna (edited April 18, 2000).]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
One more point to get this to compile - you need to have the current directory "." in your classpath. Hopefully you did that long ago, but if this is the first time you've ever compiled more than one file, perhaps it hasn't been necessary yet. If you don't know how to set classpath, go here for Windows, or here for UNIX.
Cool example, Maha. I hadn't noticed that final infinite loop possibility.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Here is some food for thought in Java :-)