Sunday, January 17, 2010

Abstract Class Program in Java

/*
Salary1.java
-------------------------------------------------------------------
WAP a Java Program to create one Abstract class i.e.
AbstractEmployee which contains Calculate() and Display() Methods.
Employee class inherits AbstractEmployee class which contains TA,DA,HRA,PF,
GS,GD and NetSalary data members.

In this case Display() is Non-Abstract and defind in abstrct class
as well as derived class. So, we have to use super keyward to call
in the subclass version of the Display(). Then super class Display()
will call first then sub class Display() will call.
--------------------------------------------------------------------
TA is 20% of Basic.
DA is 5% of Basic.
Income Tax is 15% of Basic

PF = 5000

GS = Basic+HRA+TA+DA

GD=IT+PF

Display the Employee Details.
-------------------------------------------------------------------
Question:-Can a method declared within abstract class ??
Yes, a non abstract method can be defined with an abstract class.

--------------------------------------------------------------------*/

Answer :


abstract class AbstractEmployee
{
int SlNo;
String Name;
float Basic;

AbstractEmployee(int SlNo, String Name,float Basic)
{

this.SlNo=SlNo;
this.Name=Name;
this.Basic=Basic;
}

abstract void Calculate();

abstract void test();

void Display()
{
System.out.println("APTECH");
System.out.println("NIIT");
System.out.println("SQL STAR");

}
}


class Employee extends AbstractEmployee
{
float ta,da,hra,pf,it,gs,gd,net;


Employee(int SlNo,String Name,float Basic)
{
super(SlNo,Name,Basic);
}

void Calculate()
{
ta=Basic * .20f;
da=Basic * .05f;
it=Basic * .15f;
hra=1000;
pf=5000;
gs=Basic+hra+ta+da;
gd=it+pf;
net=gs-gd;
}

void Display()
{

super.Display();

System.out.println(" SlNo of the Employee is " +SlNo);
System.out.println(" Name of the Employee is"+ Name);
System.out.println(" Basic Salary = "+ Basic);
System.out.println(" TA = " + ta);
System.out.println(" DA = " + da);
System.out.println(" HRA = " + hra);
System.out.println(" IT =" + it);
System.out.println("\n PF = " + pf);
System.out.println("\n GS = " + gs);
System.out.println("\n Net Salary = " + net);

}

void test()
{
System.out.println("Hello");
}

}


class Salary1
{
public static void main(String args[])
{
Employee e1=new Employee(1,"Saroj",25000);
e1. Calculate();
e1.Display();
e1.test();
}
}

No comments:

Post a Comment