An example of an abstract class – Employee:
An abstract class is declared using the keyword abstract. For example:
public abstract class Employee
{
...
}
Such a class cannot be instantiated: that is, you cannot create an Employee object. Abstract classes normally have at least one abstract method. An abstract method has a header, but has no implementing code associated with the header. The implementation code must be supplied by subclasses of the abstract class. The keyword abstract is also used to identify these methods. Note the difference from interfaces, which have abstract methods but do not require the word abstract in the declaration.
For example, we can define an abstract method named calculatePay within the Employee class, as follows:
public abstract int calculatePay ();
As before, we use the semicolon to indicate that no code is provided for the abstract method calculatePay. This method will be inherited by all subclasses of Employee.Concrete classes and methods A fully implemented class, with no abstract methods, is known as a concrete class. We also use the term concrete method to identify fully implemented methods, as opposed to abstract methods. It is possible for an abstract class to have one or more other abstract classes as subclasses. However, at some point in the class hierarchy the subclasses must be concrete to allow objects to be created. In our example, only Employee is abstract - all the other classes are concrete. There is no Java keyword to declare concrete classes - classes are assumed to be concrete unless they are defined as abstract.
The code for calculatePay must be provided by any concrete class that inherits from Employee. For example, the MonthlyEmployee class can be defined as follows:
public class MonthlyEmployee extends Employee
{
// new instance variables for MonthlyEmployee
// constructors for MonthlyEmployee
// any new methods for MonthlyEmployee
...
public int calculatePay ()
{
// code which calculates the
// pay of a monthly employee
}
...
}
The important point about abstract classes with abstract methods is that they place requirements on their concrete subclasses to implement certain methods. You must follow these requirements because you cannot create an object of an abstract class. However, as we shall see later, we can define a variable of an abstract class type and this is often useful.
Finally, in this section, we note that it is permissible for an abstract class to have no abstract methods and possibly a number of concrete methods. There are some useful examples of this in the Java standard packages, including the adapter classes, such as MouseAdapter.
When you should not use an abstract class
A class type should be declared abstract only if the intent is that concrete subclasses can be created to complete the implementation. If you simply want to prevent creation of objects of a particular class, the proper way to express this is as follows: declare a constructor of no arguments, make it private, never invoke it, and declare no other constructors.
A class of this form usually contains class-wide methods and variables. The class java. lang.Math is an example of a class that cannot be instantiated. It is used only to provide various mathematical functions and constants, for example:
double length = Math.sqrt(AreaOfSquare);
It does not make sense to create objects of type Math, as they would not have any stored data. So, its declaration is as follows:
public final class Math
{
private Math(){} // constructor never used
... class-wide methods and variables
...
}
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your An example of an abstract class – Employee homework and assignments? Live An example of an abstract class – Employee experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer An example of an abstract class – Employee homework help, java assignment help and An example of an abstract class – Employee projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours