aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Accessing default class methods in other packages (Bug??) 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 "Accessing default class methods in other packages (Bug??)" Watch "Accessing default class methods in other packages (Bug??)" New topic
Author

Accessing default class methods in other packages (Bug??)

Abhilash Nair
Greenhorn

Joined: Mar 29, 2006
Posts: 3
Hi,
As i was going through the access specifiers, got confused about the default access for classes. As per my understanding, if i have a class with default access, then there is no way to access the methods of this class from another class in another package. But when i tries the folloing code, i got confused since i could able to access the methods defined in a default class from another class in another package.

I have an interface defined in package p1.
package p1;
public interface Interface1{
int add(int i, int j);
}

And i have an impl class Cert1 and a public class Cert2 in the same package.

package p1;

class Cert1 implements Interface1{
protected void meth1(){
}
public int add(int a, int b){
return a+b;
}

}
public class Cert2{
public Cert1 c = new Cert1();
}

Then i have another class in another package p2 where i am calling the method defined in Cert 1.

package p2;
import p1.*;
class Cert3{
public static void main(String a[]){
Interface1 i =new Cert2().c;
System.out.println(i.add(1,2));
}
}


This way i could able to execute a method defined in a class with default access from another class in another package.Can somebody explain this?
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

Interface methods are always implicitly public and abstract. Even if you don't type public in front of the interface method, it still is!
Note that your implementing class also implements the add method as public, not default access.
Beware of questions like this:



regards,
Mark


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
gaurav singhal
Ranch Hand

Joined: Nov 18, 2005
Posts: 135
Thats right mark but the implementing class has a default access which implement the the interface so the impelemented method should not be able to accessed from outside fro the package.

I hope somebody in this forum should be able to clarify this.
Abhilash Nair
Greenhorn

Joined: Mar 29, 2006
Posts: 3
Yeah as Gaurav said, my question was not about interface and its implementation.
The point is that i could able to access methods defined in a class with derault access from a different package.
So i request forum members who unserstood the actual question to throw some light on this.
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

You are right, sorry about that.
Doesn't default access on class level mean another class outside the package can not make an instance/reference of that class (because it is not visible) ? and in your case you are not doing that either, you are creating a reference to an interface (that happens to point to your default-access-class). So you are doing nothing wrong.
Abdul Rehman
Ranch Hand

Joined: Nov 07, 2006
Posts: 65
Yes, default access means you can't access it in another package. In the above case, the class has default-access, which means that the class is not visible in other packages. But, the method add() is public and is visible as well as accessible. This means that though you can't declare a reference of that class, you can still access the add() method, if you succeed in obtaining an instance of the class indirectly. This is what you are doing: indirectly getting an instance of the default-access class. Once you have the instance, calling a public method is not a violation of any rule.

Yours,
Abdul Rehman.


SCJP 5.0 (100%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing default class methods in other packages (Bug??)