Packages in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

In this programming language, a package defines a namespace in which classes are stored. A package is nothing but a directory. Generally, similar types of files are kept in a package. You can also create a hierarchy of packages, which means a package may contain sub-packages.


Packages are not only used for organizing files, it is also used for access control of member variables and methods as a package defines a namespace.


Classes in same package: How to run the Program?

A.java
1
2
3
4
5
6
7
8
9
10
11
package test;
public class A {
int x = 10;
void showA() {
System.out.println(x);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

B.java
1
2
3
4
5
6
7
8
9
10
package test;
public class B extends A {
int y = 5;
void showB() {
System.out.println(x + " " + y);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

TestMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package test;
public class TestMain {
public static void main(String[] args) {
A a = new A();
a.showA();
B b = new B();
b.showB();
b.showA();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Classes in different package


A.java
1
2
3
4
5
6
7
8
9
10
package com.t4b.tuts.javatest.pkg1;
public class A {
public void show() {
System.out.println("I am in A");
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

B.java
1
2
3
4
5
6
7
8
9
package com.t4b.tuts.javatest.pkg2;
public class B {
public void show() {
System.out.println("I am in B");
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

TestMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.t4b.tuts.javatest;
import com.t4b.tuts.javatest.pkg1.A;
import com.t4b.tuts.javatest.pkg2.B;
public class TestMain {
public static void main(String[] args) {
System.out.println("Hello test!");
A a = new A();
a.show();
B b = new B();
b.show();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX



Naming Convention


Many developers are working on different projects in different companies or organizations. Sometimes, the developers are using some libraries that are developed by others. In this scenario, there is a high possibility of using the same name for different classes written by different developers. The same name of the classes results in a conflicting scenario when the classes are used in a project. 

 

This problem can be resolved by using packages. If you need to use one of those classes in your class, you have to import the class by its name along with the package name. Thus, the names of the packages should be unique. Hence, the developer should follow the naming convention.

 

In this context, we will discuss how to define package names.

 

Only lowercase characters are used for package names to avoid conflict with the names of classes or interfaces.

 

Generally, reversed Internet domain name is used to begin their package names.

 

Name collisions that may occur within a single company can be handled by using the project name after the reversed domain name.

 

Note that in some cases, the internet domain name may not be a valid package name, if it contains a special character (other than a hyphen) or it begins with a digit.


Example


Let us discuss the previous example of the banking system.

Account.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.abcbank.account;
public abstract class Account {
private int _accountNo;
private String _customerName;
private String _customerAddress;
private String _branchName;
private String _branchAddress;
private float _amount;
private float _rateOfInterest;
public int get_accountNo() {
return _accountNo;
}
public void set_accountNo(int _accountNo) {
this._accountNo = _accountNo;
}
public String get_customerName() {
return _customerName;
}
public void set_customerName(String _customerName) {
this._customerName = _customerName;
}
public String get_customerAddress() {
return _customerAddress;
}
public void set_customerAddress(String _customerAddress) {
this._customerAddress = _customerAddress;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

SavingsAccount.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.abcbank.account.savings;
public class SavingsAccount extends Account {
public void payAnnualInterest() {
set_amount(get_amount() + get_amount() * get_rateOfInterest() / 100);
}
public void deposit(float amount) {
set_amount(get_amount() + amount);
}
public void withdrawl(float amount) {
set_amount(get_amount() - amount);
}
public float getAccountBalance() {
return get_amount();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

LoanAccount.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.abcbank.account.loan;
public abstract class LoanAccount extends Account {
public float getRemainingLoanAmount() {
return get_amount();
}
public void rePayment(float amount) {
set_amount(get_amount() - amount);
}
public abstract void updateLoanAmountOnInterest(int noOfYear);
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

HomeLoanAccount.java
1
2
3
4
5
6
7
8
9
10
11
package com.abcbank.account.loan;
public class HomeLoanAccount extends LoanAccount {
// update loan amount based on compound interest
public void updateLoanAmountOnInterest(int noOfYear) {
set_amount(get_amount() + get_amount() * (float) Math.pow(get_rateOfInterest() / 100, noOfYear));
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

EducationLoanAccount.java
1
2
3
4
5
6
7
8
9
10
11
package com.abcbank.account.loan;
public class EducationLoanAccount extends LoanAccount {
// update loan amount based on simple interest
public void updateLoanAmountOnInterest(int noOfYear) {
set_amount(get_amount() + get_amount() * get_rateOfInterest() * noOfYear / 100);
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.