Creating packages and derived classes in Java : An illustration

This program is a simple illustration of creating packages in Java and derived classes within the same package.

A package in Java can be defined as a directory which contains a group of Java class files. The main advantage of this method is that any no. of derived classes can be created within the same package.

Below is a program which uses packages to show the details of an employee.

Package 1: Office

In the office directory there are two Java files - emp.java & dept.java

emp.java


    package office;
    public class emp
    {
        public String name,address,acc_no;
        public emp(String nm,String add,String acc_no2)
        {
            name=nm;
            address=add;
            acc_no=acc_no2;
        }
    }


dept.java

    package office;     /* no need to use import within the same package  */
    public class dept extends emp
    {
        public String dept,desig;
        public dept(String nm,String add,String acc,String deptm,String desg)
        {
            super(nm,add,acc);
            dept=deptm;
            desig=desg;
        }
    }

It is advisable to keep the code within the directory as when you compile it the class files will also be in the same directory.

To create the class use the following the commands :

javac emp.java
javac dept.java

If you face any problem while implementing the above command, you can also use the commands below :
javac office/emp.java
javac office/dept.java

Now, you have successfully created your first package.

Package 2 : Finance


In the finance package, there is a single java file - salary2.java

salary2.java


    package finance;
    import office.emp;     /*use import to create derived class of outside 
                                  package */
    import office.dept;
    public class salary2 extends dept
    {
        private float basic,hr,da,saly;
        public salary2(String nm,String add,String acc,String deptm,String   
                                  desg,float bas,float h_r,float d_a)
        {
            super(nm,add,acc,deptm,desg);
            basic=bas;
            hr=h_r;
            da=d_a;
            saly=basic + (float)0.01*hr + (float)0.01*da;
        }
        public void display()
        {
            System.out.println("Name    : " + name);
            System.out.println("Address : " + address);
            System.out.println("Acc No. : " + acc_no);
            System.out.println("Department : " + dept);
            System.out.println("Designation : " + desig);
            System.out.println("Basic Pay : " + basic);
            System.out.println("DA : " + hr + "%");
            System.out.println("HR : " + da + "%");
            System.out.println("Total Salary : "+ saly);
        }
    }

Now to create the class files use the following commands :

javac salary2.java   or   javac finance/salary2.java

Now create a normal java outside of both the office and finance directory from where you can call the methods to show the details of the employees.

abc.java

    import finance.salary2;
    public class abc
    {
        public static void main(String args[])
        {
            salary2 sal=new salary2("Rohit","265,G.C.Road,Kolkata- 
            02","97832","SALES","CEO",8000,8,10);
            sal.display();
        }
    }

Now the run the following commands to get the final output.

javac abc.java
java abc

The following output will be shown :

Name    : Rohit
Address : 265,G.C.Road,Kolkata-02
Acc No. : 97832
Department : SALES
Designation : CEO
Basic Pay : 8000.0
DA : 8.0%
HR : 10.0%
Total Salary : 8000.18



Note :  To create a derived class in the same package you don't need to use the import statement. You will need the import statement only when creating a derived class of an outside package.

Top