Java abstract class definition and method instance detail

  • 2020-06-23 00:30:02
  • OfStack

In the object-oriented concept, all objects are depicted by classes, but conversely, not all classes are used to depict objects, if a class does not contain enough information to depict a concrete object, such a class is an abstract class.

Except that an abstract class cannot instantiate an object, other functions of the class remain, with member variables, member methods, and constructors accessed in the same way as a normal class 1.

Since an abstract class cannot instantiate an object, it must be inherited before it can be used. It is also for this reason that the decision to design abstract classes is usually made at design time.

Superclasses contain common methods for a collection of subclasses, but because the superclasses themselves are abstract, they cannot be used.

An abstract class

In the Java language, abstract class is used to define abstract classes. Examples are as follows:


/*  The file name  : Employee.java */
public abstract class Employee
{
  private String name;
  private String address;
  private int number;
  public Employee(String name, String address, int number)
  {
   System.out.println("Constructing an Employee");
   this.name = name;
   this.address = address;
   this.number = number;
  }
  public double computePay()
  {
   System.out.println("Inside Employee computePay");
   return 0.0;
  }
  public void mailCheck()
  {
   System.out.println("Mailing a check to " + this.name
    + " " + this.address);
  }
  public String toString()
  {
   return name + " " + address + " " + number;
  }
  public String getName()
  {
   return name;
  }
  public String getAddress()
  {
   return address;
  }
  public void setAddress(String newAddress)
  {
   address = newAddress;
  }
  public int getNumber()
  {
   return number;
  }
}

Notice that the Employee class is no different, although it is an abstract class, it still has three member variables, seven member methods, and one constructor. Now if you try the following example:


/*  The file name  : AbstractDemo.java */
public class AbstractDemo
{
  public static void main(String [] args)
  {
   /*  This is not allowed and will cause an error  */
   Employee e = new Employee("George W.", "Houston, TX", 43);
   System.out.println("\n Call mailCheck using Employee reference--");
   e.mailCheck();
  }
}

When you try to compile the AbstractDemo class, you get the following error:


Employee.java:46: Employee is abstract; cannot be instantiated
   Employee e = new Employee("George W.", "Houston, TX", 43);          ^
1 error

Inherited abstract class

We can inherit the Employee class through a 1-like method:


/*  The file name  : Salary.java */
public class Salary extends Employee
{
  private double salary; //Annual salary
  public Salary(String name, String address, int number, double
   salary)
  {
    super(name, address, number);
    setSalary(salary);
  }
  public void mailCheck()
  {
    System.out.println("Within mailCheck of Salary class ");
    System.out.println("Mailing check to " + getName()
    + " with salary " + salary);
  }
  public double getSalary()
  {
    return salary;
  }
  public void setSalary(double newSalary)
  {
    if(newSalary >= 0.0)
    {
     salary = newSalary;
    }
  }
  public double computePay()
  {
   System.out.println("Computing salary pay for " + getName());
   return salary/52;
  }
}

Although we cannot instantiate an object of an Employee class, if we instantiate an Salary class object, it inherits three member variables and seven member methods from the Employee class.


/*  The file name  : AbstractDemo.java */
public class AbstractDemo
{
  public static void main(String [] args)
  {
   Salary s = new Salary("Mohd Mohtashim", "Ambehta, UP", 3, 3600.00);
   Employee e = new Salary("John Adams", "Boston, MA", 2, 2400.00);
   System.out.println("Call mailCheck using Salary reference --");
   s.mailCheck();
   System.out.println("\n Call mailCheck using Employee reference--");
   e.mailCheck();
  }
}

The compilation and operation results of the above program are as follows:


Constructing an Employee
Constructing an Employee
Call mailCheck using Salary reference --
Within mailCheck of Salary class
Mailing check to Mohd Mohtashim with salary 3600.0
Call mailCheck using Employee reference--
Within mailCheck of Salary class
Mailing check to John Adams with salary 2400.

Abstract methods

If you want to design a class that contains a particular member method whose implementation is determined by its subclasses, you can declare the method abstract in the parent class.

The Abstract keyword can also be used to declare abstract methods that contain only one method name and no method body.

Abstract methods are not defined, and the method name is followed by a semicolon instead of curly braces.


public abstract class Employee
{
  private String name;
  private String address;
  private int number;
   public abstract double computePay();  
  // The rest of the code 
}

Declaring an abstract method results in two things:

If a class contains abstract methods, it must be an abstract class.

Any subclass must override the abstract methods of the parent class or declare itself an abstract class.

A subclass that inherits an abstract method must override it. Otherwise, the subclass must also be declared abstract. Finally, a subclass must implement the abstract method, otherwise neither the original parent class nor the final subclass can be used to instantiate the object.

If the Salary class inherits the Employee class, then it must implement the computePay() method:


/*  The file name  : Salary.java */
public class Salary extends Employee
{
  private double salary; // Annual salary 
  public double computePay()
  {
   System.out.println("Computing salary pay for " + getName());
   return salary/52;
  }
  // The rest of the code 
}

I hope this article can help those in need


Related articles: