In: Computer Science
Assume you have created the following data definition class:
public abstract class Organization {
private String name;
private int numEmployees;
public static final double TAX_RATE = 0.01;
public String getName() { return this.name; }
public int getNumEmployees() { return
this.numEmployees; }
public abstract double calculateTax() {
return this.numEmployees *
TAX_RATE;
}
}
In your own words, briefly (1-2 sentences) explain why the data definition class will not compile. Then, write modified code that addresses the issue.
This given Organization class is abstract class. Abstract class has at least one abstractmethod and abstract method does not have body. So, the given code won't compile.
Modified Code :
public
abstract class Organization {
private String name;
private int numEmployees;
public static final double TAX_RATE = 0.01;
public String getName() { return this.name; }
public int getNumEmployees() { return this.numEmployees;
}
public abstract double calculateTax()
{
return
this.numEmployees * TAX_RATE;
}
}
The text in bold is the issue because this method is abstract method and has body. That's not possible. To solve this issue, you can create a subclass of the Organization and in which you can define a body of the abstract method.
public
abstract class Organization {
private String name;
private int numEmployees;
public static final double TAX_RATE = 0.01;
public String getName() { return this.name; }
public int getNumEmployees() { return this.numEmployees;
}
public abstract double
calculateTax();
}
XYZ.java - this is a subclass of Organization class
public class XYZ extends Organization
{
public double calculateTax()
{
return this.numEmployees * TAX_RATE;
}
}
Second solution is that you can remove abstract keyword like below.
public
class Organization {
private
String name;
private int numEmployees;
public static final double TAX_RATE = 0.01;
public String getName() { return this.name; }
public int getNumEmployees() { return this.numEmployees;
}
public double calculateTax() {
return
this.numEmployees * TAX_RATE;
}
}