Sunday 16 June 2013

Abstract class program in java

abstract class Employee
{
    private int eno;
    private String name;
    private float bs;
    private int hra,da,ta;
    private float HRA,DA,TA,gs;
    public Employee()
    {
        eno=0;
        name="Empty";
        bs=0.0f;
        hra=ta=da=0;
    }
    public Employee(int eno,String name,float bs,int hra, int da,int ta)
    {
        this.eno=eno;
        this.name=name;
        this.bs=bs;
        this.hra=hra;
        this.da=da;
        this.ta=ta;
    }
    public float getSal()
    {
        HRA=(bs*hra)/100.0f;
        DA=(bs*da)/100.0f;
        TA=(bs*ta)/100.0f;
        gs=HRA+DA+TA+bs;
        return gs;
    }
    abstract public void calsal();
}
class WageEmp extends Employee
{
    private int hours,rate;
    public WageEmp()
    {
        hours=0;
        rate=0;
    }
    public WageEmp(int eno,String name,float bs, int hra,int da,int ta,int hours,int rate)
    {
        super(eno,name,bs,hra,da,ta);
        this.hours=hours;
        this.rate=rate;
    }
    public void calsal()
    {
        System.out.println("salary -"+getClass()+(hours*rate));
    }
}
class Manager extends Employee
{
    private int inc;
    public Manager()
    {
        inc=0;
    }
    public Manager(int eno, String name,float bs, int hra,int da,int ta,int inc)
    {
        super(eno,name,bs,hra,da,ta);
        this.inc=inc;
    }
    public void calsal()
    {
        System.out.println("\n Salary - "+getSal()+inc);
    }
}
class EmpDemo
{
    public static void main(String args[])
    {
        WageEmp we=new WageEmp(1,"Amit",2000.0f,9,8,8,10,5);
        Manager mgr=new Manager(111,"Ajay",10000.0f,11,11,13,5550);
        Employee []arr=new Employee[2];
        arr[0]=we;
        arr[1]=mgr;
        for(int i=0;i<arr.length;i++)
            arr[i].calsal();
    }
}

No comments: