by Dinesh Thakur

Some time there is a situation in which you feel the need of a superclass that has only declaration of few or all methods, with definition of few or none methods. But it is necessary that it must not be a completely defined or implemented class. The methods are declared only and specified by abstract type modifier called abstract method. It is the responsibility of the concrete subclass to implement all abstract method of superclass. To declare any method as abstract use the following form: abstract accessmodifer retumtype methodName( <parameterlist>);

 

A class can have one or more abstract methods, that class must also be declared as abstract. To declare a class as abstract, simply add abstract keyword before the class keyword. Abstract class cannot be instantiated, i.e. you cannot create an object with the new operator of abstract class. The construct cannot be abstract, and no method can be abstract static. Any concrete subclass has to implements all the abstract method of its abstract superclass. Abstract classes can be used to create object references.

 

Example:

 

abstract class Employee

{

private int Empid;

private String EmpName;

public Employee(int eid, String ename)

{

Empid = eid;

EmpName = ename;

}

public void display()

{

System.out.println("Employee Name is: " + EmpName);

System.out.println("Employee ID is: " + Empid);

}

abstract void calcSalary();

}

class EmpAdhoc extends Employee

{

 

private int noofworkingdays;

private float dailywages;

private float netsalary;

public EmpAdhoc(int eid, String ename, int workday, float wages)

{

super(eid, ename);

noofworkingdays= workday;

dailywages=wages;

calcSalary();

}

public void calcSalary()

{

netsalary=noofworkingdays * dailywages;

 

}

public void display()

{

super.display();

System.out.println("No. of working days :: " + noofworkingdays);

System.out.println("Daily wages :: " + dailywages);

System.out.println("Net Salary:: " + netsalary);

}

}

class Salaried extends Employee

{

private float hra;

private float da;

private float basicsalary;

private float netsalary;

Salaried(int eid, String ename, float basicsal)

{

super(eid, ename);

basicsalary = basicsal;

calcSalary();

}

public void display()

{

super.display();

System.out.println("Basic Salary:: " + basicsalary);

System.out.println("HRA:: " + hra);

System.out.println("DA :: " + da);

System.out.println("Net Salary:: " + netsalary);

}

public void calcSalary() //Definition of Abstract Method

{

hra=basicsalary* 0.40f;

da=basicsalary* 0.60f;

netsalary= basicsalary+hra+da;

}}

 

public class AbstractDemo {

public static void main(String arg[]){

EmpAdhoc eaObj = new EmpAdhoc (1001,"Alice",25,600.00F);

eaObj.display();

Salaried salObj = new Salaried (2001,"Bob",60000.00F);

salObj.display();

}

}

 

Output:

 

Employee Name is: Alice

Employee ID is: 1001

No. of working days :: 25

Daily wages :: 600.0

Net Salary:: 15000.0

Employee Name is: Bob

Employee ID is: 2001

Basic Salary:: 60000.0

HRA :: 24000.0

DA :: 36000.0

Net Salary :: 120000.0