Packages in Java - BunksAllowed

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

Random Posts


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
package test; public class A { int x = 10; void showA() { System.out.println(x); } }
B.java
package test; public class B extends A { int y = 5; void showB() { System.out.println(x + " " + y); } }
TestMain.java
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(); } }


Classes in different package


A.java
package com.t4b.tuts.javatest.pkg1; public class A { public void show() { System.out.println("I am in A"); } }
B.java
package com.t4b.tuts.javatest.pkg2; public class B { public void show() { System.out.println("I am in B"); } }
TestMain.java
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(); } }




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
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; } public String get_branchName() { return _branchName; } public void set_branchName(String _branchName) { this._branchName = _branchName; } public String get_branchAddress() { return _branchAddress; } public void set_branchAddress(String _branchAddress) { this._branchAddress = _branchAddress; } public float get_amount() { return _amount; } public void set_amount(float _amount) { this._amount = _amount; } public float get_rateOfInterest() { return _rateOfInterest; } public void set_rateOfInterest(float _rateOfInterest) { this._rateOfInterest = _rateOfInterest; } }
SavingsAccount.java
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(); } }
LoanAccount.java
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); }
HomeLoanAccount.java
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)); } }
EducationLoanAccount.java
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); } }


Happy Exploring!

No comments:

Post a Comment