![]() |
Let us first know what is a class and package in Java. Class in java is a model for creating objects. It means that the properties and actions of the objects are written in class. Properties are represented by variables and actions of the objects are represented by methods. So, a class contains variables and methods. The same variables are also available in the objects because they are created from the class. These variables are also called ‘instance variables’ because they are created inside the object (instance). Illustration: class Human { // Properties --> Instance variables String name; int age; // Actions --> methods void speak() { System.out.println("Hey there! I am " + name); System.out.println("My age is " + age); } } Output: Hey there! I am Vasanth Kumar My age is 20
Packages in Java In Java, Packages are a collection of classes, sub-packages, and interfaces. i.e A package represents a dictionary that contains a related group of classes and interfaces. Whenever we write a statement be it as shown below we are importing all the classes of java.io.* package. Here, java is a directory name and io is another sub directory within it. And the * represents all the classes and interfaces of that io subdirector. import java.io.*; We have many such packages, for example, java.lang, java.util, and there do exist many more inside which their d lies classes inside the package. In order to use so let us do take the most frequently used packages that are the ‘lang’ package for java designing and the ‘io’ package for input-output operations. Let us discuss advantages of packages in java hat are as follows:
Types of packages There are two types of packages in java as listed:
Type 1: Built-in Packages These are the packages that are already available in the Java language. These packages provide nearly all necessary classes, interfaces, and methods for the programmer to perform any task. They are as follows:
Type 2: User-defined Packages Just like the built-in packages shown earlier, the user of the java language can also create their own packages. They are called User Defined Packages. These packages can also be imported into other classes and used exactly in the same way as built-in packages. Syntax: Creating a package, package keyword is used 2.1.1 To create a package package package_name ; 2.1.2 To create a subpackage within a package package package_name.subpackagename ; 2.2 Compile C:\> javac -d . classname.java 2.3 To run the program C:\> java packagename.classname Above syntax is valid only for windows CMD shell, fo rlinux and mac zsh shell refer to below media as perceive it the same way provided below Implementation: Now let us divide the class into packages Step 1: Define a class StudentData that holds the following information of a student:
Note that these fields should be declared private. Step 2: Create another class StudentDataExtended with a private attribute named location ( a String that stores the location of the student). Step 3: Now in this class define a method addDetails() method that stores the details of students; and a method printDetails() method that outputs the details of students in the sorted order of their id.
Illustration: Enter the number of students : 2 Enter the details of Student 1 (id, name, location): B200 Ajay Hyderabad Enter the details of Student 2 (id, name, location): B100 Ramesh Hyderabad The Student Details are: B100 Ramesh Hyderabad B200 Ajay Hyderabad Example Java
Java
Output: |
Reffered: https://www.geeksforgeeks.org
Java |
Related |
---|
![]() |
![]() |
|
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |