In this course, we'll learn the object-oriented concept in Java.
- Object-Oriented programming concept
- Object & Class
- Access Modifiers
- Naming Conventions
- Static Keyword
- Nested and Inner Classes
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language
- Basic computer knowledge
Hi there. In this video, we'll 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 size 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's the vehicle is a top-level package. The car is a sub-package. There are some rules to define packages. 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. Types of packages. We have two types of packages in Java.
Number one, Built-in package. The already defined package like java.lang, java.io, etc. are known as built-in packages. 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 inbuilt 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 package_name.class_name, then only this class will be available for use.
For example, as you see on this slide, if we use import vehicle.car.Ferrari, that means we will use only Ferrari class in sub-package 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 interface 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 make some examples with packages. In fact, we did the implementation of the package in all examples, but we need to learn the package concept in detail. In the object-oriented programming project, right-click on the source folder and select 'New,' 'Package.' Specify the package name as packageoperation. Let's create a new class. Right-click on the packageoperation package and select 'New', 'Class.'
Specify class name as Multiply. It means that multiply class in packageoperation package. I will not take the checkbox for the main method. Let's start to code. Let's declare an int method, multiply with two parameters, a and b. Public int multiply (int a, int b). In this method, we will return the a*b. Now, let's create a new package for the test. I right-click on the source folder an select 'New' and 'Package.' Specify the package name as packagetest. Let's create a new class. Right-click on the package test package and select 'New,' 'Class.' Specify the class name as Test and select the check box to add the main method. First, we need to import the scanner class for getting input from the user. Scanner input = new Scanner(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. For packages in Java and classes in these packages, you can check the link I shared for this lesson.
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. And now, I'll show a message to the user to enter two numbers by using the print method. S.out.printIn('' Please enter two numbers:''). Let's declare two variables named number one and number two with int type and I'll assign next int methods of the scanner class to these variables; int number1= input.nextInt(), Int 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 packageoperation.'
It's added after the scanner class definition. This is a user-defined package. Let me add a comment. Let's print the multiplication of numbers, S.out.printIn(''Results =" + multi.multiply(number1, number2) as parameters. Let's run the code. I will enter 12 and 8. Multiply method in the Multiply class is called, and it displays result is 96. Let's try to change the access modifier of the multiply method in the Multiply class. I'll change the public to private and I'll 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. I 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.
Now, the access modifier is public and we can use this method from other packages. Yes. So, we remembered 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 wit two int parameters, a and b, public int add (int a, int b) We will return the a+b expression, and I'll save the class. Let's open the main class test. We have two classes in the package operation package, Multiply and Add. To use these two classes 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 is no need to import this class anymore. The asterisk represents all classes in the package operation package.
Now let's print the sum of numbers. I'll copy and print this method and paste it here. Also, this method will be Add, not Multiply. 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 is 96. And the Add method in the add class is called and it displays result is 20. Yes. Before I conclude the lesson, I'd 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 are 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 otherpackageoperation. Let's create a new class in this package. I right-click on the 'otherpackageoperation' package and select 'New', 'Class.' Specify the class name as Add and click 'Finish'. Now, we have two classes named Add, but they are in different packages. Now, in the test class, I'll 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 in directly.
This is to make it clear which Add class belongs to which package. Now, you will understand better what I mean. First, let's import the other package operations 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 is unclear which package this Add class belongs to. Now, let's go one step further. Let's say we import only the Add class, not all the classes of the other package operations 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 and 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. Yes. As you can see, the first Add class is now from the packageoperations package, and the second is from the otherpackageoperations package. As a result, 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. Yes. In this lesson, we focused on the concept of package and import. Let's take a short break here. See you in the next lesson.
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.