Packages and Imports

Contents

Object-Oriented Programming
1
Overview
PREVIEW36s
2
What is OOP?
PREVIEW3m 44s

The course is part of this learning path

Start course
Difficulty
Beginner
Duration
1h 27m
Students
38
Ratings
5/5
starstarstarstarstar
Description

In this course, we will learn the concepts of microservice and spring framework with a focus on object-oriented programming.

Learning Objectives

  • Object-oriented programming in Java

Intended Audience

  • Beginner Java developers
  • Java developers interested in learning how to Build and Deploy RESTful Web Services
  • Java Developers who want to develop web applications using the Spring framework
  • Java Developers who want to develop web applications with microservices
  • Java Developers who wish to develop Spring Boot Microservices with Spring Cloud

Prerequisites

  • Basic Java knowledge
Transcript

Hello there, my friend. In this video, we will talk about packages and imports. A Java package is a group of similar types of classes, interfaces, and other sub-packages. If you have a large-sized project that consists of hundreds of Java classes, you need to organize them in packages. You can think of the packages like file directories on your PC. This will allow you to categorize files, control access to your classes, and avoid potential naming conflicts. Classes with the same name can be put into different packages. So, packages avoid name collision. Package keyword is used to declare a package. In our example, we declare a package vehicle.car; Here, the vehicle is a top level package, the car is a subpackage. There are some rules to define packages like this. Package declaration is done at the beginning of the source file. Only one package declaration can be done per source file. If no package is declared then the class belongs to the default package. The package name must be hierarchical and separated by dots.

Here are the types of packages. So, we have two types of packages in Java. One, built-in package, the already defined package like java.lang, java.io etc., are known as built-in packages. And number two, user-defined package. The package we create is called the user-defined package. For example, the vehicle.car is a user-defined package. Import keyword. We use the import keyword to import in-built and user-defined packages into our Java source file. Our class can refer to a class that is in another package and we can use its name directly in your class. We can access the classes and interface of another package with the import keyword. We have two different ways to import any class in the different packages. First one, if you import packagename.classname, then only this class will be available for use.

For example, as you see on the slide, if we use import vehicle.car.Ferrari; that means we will use only Ferrari class in subpackage car in top package vehicle. The second way to import, if you use package name .*, then all the classes and interfaces of this package will be accessible but the classes and interfaces inside the sub-packages will not be available for use. There is one exception to the import rule. All classes in the java.lang package are imported by default. Thus, we do not need to import classes in the java.lang package in our code. The system class we used in the scanner class can be given as an example of this exception. System class is predefined in the java.lang package, so we use it in our code without importing it. Sequence of program. As you see in the diagram in Java, a Java source file can contain any or all of the following three internal parts: A package statement, import statements, a public class declaration. A Java source file should have a public class declaration; all other statements are optional. The program should be written in a flow package with imports and class.

Let's look at some package examples. In fact, we implemented the package in all examples. However, we must thoroughly understand the package concept before we move on. In the object-oriented programming project, right click on the source folder and select new package. Specify the package name as packageoperations. Okay, let's create a new class. Right click on the packageoperations package and select new class. Specify class name as Multiply, it means multiply class in the packageoperations package. I will not tick the checkbox for the main method. All right, let's start to code. Let's declare an int method. Multiply with two int parameters a and b, public int multiply(). In parentheses, I write int a and int b. In this method, we will return the a*b; Okay, now let's create a new package for the test. I right click on the source folder and select new and package. Specify the package name as packagetest. Okay, let's create a new class. Right click on the packagetest package and select new class.

Specify the class name as Test and select the checkbox to add the main method. Okay, first we need to import the Scanner class for getting input from the user, Scanner input = new Scanner(). In parentheses, I write System.in. Since the system package is in the java.lang package, we don't need to import it. But for the Scanner class, we need to import the java.util package. We choose the import Scanner class in java.util package to import the Scanner class. So, the java.util is a built-in package. Let me add a comment. Now I want to add a comment. I will show a message to the user to enter two numbers by using the print method. System.out.println("Please enter two numbers: "); Let's declare two variables named number1 and number2 with int type, and I will sign next int methods of the Scanner class to these variables. int number1 = input.nextInt(); number2 = input.nextInt();.

Now let's create an object from the multiply class by using the new operator, Multiply multi = new Multiply(); The Multiply class is not part of this package, so we got an error, so we have to import this class. In order to import this class, place the mouse pointer on the Multiply word and import options are shown. We choose the first option, import Multiply in packageoperations. Okay, it is added after the Scanner class definition. This is a user-defined package, so let me add a comment. Let's print the multiplication of numbers. System.out.println("Result =" +). After the plus sign, I will call the Multiply method of the Multiply class using the multi-object and I will pass number1 and number2 as parameters. Okay, let's run the code. I will enter 12 and 8. Multiply method in the Multiply class is called and it displays the result as 96. Let's try to change the access modifier of the Multiply method in the Multiply class. I will change the public to the private and I will save the class.

As you can see, this time we get an error in the main method because the access modifier of the Multiply method is private and it's not visible to other classes anymore. In these cases, Eclipse shows a quick fix dialog. This dialog provides a list of possible corrections. If we want to solve this problem, select the change visibility of Multiply method to public option. Let's open the multiply class and see the changes. Now the access modifier is public and we can use this method from other packages. So, as you can see, we remember the issue of access modifiers once again. Now, let's create a new class. I will specify the class name as Add. Let's declare an int method add with two int parameters a and b. Public int add(). In parentheses, I write int a and int b. We will return the a+b expression and I will save the class. Let's open the main class test. We have two classes in the packageoperations package: multiply and add. To use these two methods, instead of importing them separately, if we use an asterisk mark after the package definition, we can access all the classes in this package.

Now, let's create an object from class add. Add myAdd = new Add(); As you can see, we didn't get an error, so there's no need to import this class anymore. The asterisk represents all classes in the packageoperations package. Okay, now let's print the sum of numbers. I will copy this print method and paste it here. Also, this method will be add, not multiply. Okay, let's run the code. I will enter 12 and 8 again. As you can see, the multiply method in the multiply class is called and it displays result as 96, and the add method in the add class is called and it displays result as 20. So, before I conclude the lesson, I would like to mention the name conflict. Java also has classes with the same name, although they are in different packages. For example, the timer class. If we write timer here, we can see that this class is in the java.util package, the javax.management.timer package and the javax.swing package. Let's give another example. If you write date here, we can see that this class is in the java.util package and java.sql package. Although these classes have the same names, they're in different packages.

So, can we import classes with the same name but in different packages? If we can import what should we pay attention to? Let's look for answers to these questions together. First, I will create a new package. I right click on the source folder and select new and package. Specify the package name as otherpackageoperations. Okay, let's create a new class in this package. I right click on the otherpackageoperations package and select new class. Specify the class name as Add and click 'Finish'. Okay, now we have two classes named Add, but they are in different packages. Now in the test class, I will try to access these classes. We have already defined the Add class in the packageoperations package here. Now let's import the Add class from the otherpackageoperations package. When I type Add here, if you pay attention, we can see the Add classes in both packages. Here, I select the class I want to import and press the 'Enter' key on the keyboard.

But if you notice a different import has occurred this time. So, instead of creating it above with the import keyword, it used the name of the package it was indirectly. This is to make it clear which Add class belongs to which package. So, now you'll understand better what I mean. First, let's import the otherpackageoperations package using the import keyword above. First of all, I will use asterisk. Notice that this time the first Add class is underlined and we get a compilation error. If you position your mouse over the Add class, you will see this warning, "the type Add is ambiguous," so it's unclear to which package this Add class belongs. So, let's go one step further. Let's say we import only the Add class, not all the classes of the otherpackageoperations package. That's why I'm writing Add here. If you pay attention, the compilation error has disappeared.

But if you position your mouse over the Add class, this time it shows that this class is taken from the otherpackageoperations package, not the packageoperations package. The best way to fix this is to specify here which package the Add class is from. So, just before the Add class, I write packageoperations. Now as you can see, the first Add class is now from the packageoperations package and the second is from the otherpackageoperations package. In other words, you need to know that Java has classes with the same name, although they are in different packages. When you need to use these classes at the same time, it's best to use the relevant class with the package name to avoid confusion. So, that's the lesson. We focused on the concept of package and import. Let's take a short break here and I'll see you in the next lesson.

 

About the Author
Students
1963
Courses
64
Learning Paths
4

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.