Salaam all!
I have an interface declared as "public" in a package called "package1". I want to use its methods in another package "package2" so do I have to import the package1 in the package2 ? both are in the same application
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Salaam all!
I have an interface declared as "public" in a package called "package1". I want to use its methods in another package "package2" so do I have to import the package1 in the package2 ? both are in the same application
What happened when you tried without the import? Was there an error or did it compile?
shaz (November 8th, 2012)
Yes we need to import. Only when its imported then comes the part whether it can be accessible depending on the visibility(private, public, protected)
shaz (November 8th, 2012)
Yes without importing the netbeans editor shows a message that "add import for package1.<interface name>"
--- Update ---
but I read that with default modifier( no modifier) we can only access an interface in the same package but with public it can be accessed by any package? Can you explain this please
Sometimes there arises a need when a class that needs to be accessed from within the same package then default modifier is used.
For example : a third party library/jar which has a class used by other classes only within that package and it does not wants the consumers/users to use it.