Reviewing Java Packages and Their Associated Problems by Proposing Our Sample Project

Basics of Java 9 Modularity (JPMS or Project Jigsaw) Chapter 2- Some Problems We Face Using Only Packages Before Java 9
5 minutes
Share the link to this page
Copied
  Completed

Transcript

Okay, now let's start with your examples which I want to show you step by step, what do we mean by modules and why we need them. As you see I'm working inside Eclipse environment. And to the left you see the projects I provided you with. And I'm gonna start with the first one which is named j PMS basics one. So when I expand this one is you see in this project, I have a source folder which inside that one, let's start the first package calm that is to cash courses. So as you know, we use packages as namespaces for having better organization for our classes.

And as you know, the name of the packages in Java is traditionally the reverse domain name. So let's say if my website is is application.com. Then I have these as the reverse of the one for my package name. And let's say under that one, I have a soft package courses. So I have these package comm that is the cash dot courses. What I'm trying to show you is that I'm going to have some kind of simulation on my website.

Let's say I have a course as an interface which is going to be something Kind of interface which my other courses like draft courses or published courses are going to be implementing. So again, I have this package comm that is occasio dot courses. And in this package I have this interface named course. Now note that the access modifier for this interface, I have made that one package. And as you know, as long as I haven't said anything in the beginning, like public or private or protected, it means that this interface has the access modifiers package. And it means that all the other classes in the same package have directly access to this interface.

There is just one method in here would play which is going to be implemented in the courses which implement this interface. So let's say as an example, I have a class in here, which is named draft course. And it's going to be implementing the course interface. It has just one private string name. It has a draft course constructor, which takes a name and assigns that one to the Name field of this object and it has a good name which simply returns the name and it has word play method implemented in this case, I'm just trying to simulate in playing the video, I just printed the ways video percent s, which is going to be replaced by the getname method output is being played. So first things first, is it a good practice in general like to have each class and interface in its own file.

So most of the time, we don't see professional programmers do kind of coding like this, we have more than one type in a java file. So the best practice would be, let's say, I come in this out again, the best practice would be I have a second file in the same package. Now this one is going to be containing my class in the same package. I have a class which implements course, and it has the same kind of thing we talked about the question that how this class has access to this course interface is that they're both in the same package. And again, that's because of the package access. Now let's assume for many different reasons, like again, organizing your code, you want to have a second package.

Let's say in this case, I have the second package. Conduct easy to catch courses dot draft again a small package of calm that easy to catch courses. And I want to put the draft course inside this package inside the same package as the course interface. So let's open up this one. And now this time let's forget about the draft course class that we have in the same package as the course interface. At that time a listing we put the raft course on there, the new sub package of common easy to catch courses.

Now, this time if I want to have the half course implementing the course interface in the other package, as you see there is an error which says course cannot resolve to a type. The reason is obvious. We have declared the course to have package access. And draft course this time is located inside another package, although it's a sub package, but it's considered to be another package. Therefore, it can have access to the course interface. Now, one might say what if I import a course interface from the auto package?

Let's say I bring these code back as regular code. Now again, as you see here, we still have the same problem. It says we can't import the scores from the package. If you look in the message, it says the type column that is the casual courses course is not visible. The reason if you come back to here, this package access is the reason. So what if you come here and say, I declare this one to be public?

Now, this time, the access modifier is not packaged anymore, it's going to be declared as public. And if you return back to your code, the problem seems to resolve but now there is one big question you like the course interface to be declared inside this package, but if you say the access modifier is public, now your course interface is going to be visible to all the classes which import this package. You don't have any solution to hide this course interface can be just used inside its own package. When you say it's public. It could be used by any other class or any other interface in the other packages which input the Course interface from this package and as you see, the problem is we can treat the packages the same way we treat the classes we can say if this package or the things inside this package should be visible to just some specific packages.

Let's say we don't want to allow this one to be visible by some other packages, once you say it's public is going to be visible by all the types from the other packages which import your class or interface in this case from its package. Having said this, let's continue with a more comprehensive example in the next video.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.