Java —— Abstract classes

Posted by shneoh on Thu, 16 May 2019 18:02:56 +0200

Article Directory

Java - abstract class

abstract class

When reviewing design patterns, I found that many abstract classes were used for classes and wrote a blog to record learning.

Basic Definitions

  • If a class does not contain enough information to portray a specific object, it is an abstract class.
  • An abstract class cannot be instantiated, but it can be inherited by subclasses, which use its methods and member variables.

Example

  • Employee (abstract class - employee class)
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);
    }

    @Override
    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;
    }

}
  • Salary (salary class - inherit abstract class employee class)
public class Salary extends Employee{

    //Annual salary (annual output)
    private double salary;

    public Salary(String name, String address, int number, double
            salary) {
        super(name, address, number);
        setSalary(salary);
    }

    @Override
    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;
        }
    }

    @Override
    public double computePay() {
        System.out.println("Computing salary pay for " + getName());
        return salary / 52;
    }
}
  • AbstractDemo
public class AbstractDemo {
    public static void main(String[] args) {
        // Abstract classes are not allowed to be instantiated!
//        Employee employee = new Employee();
        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();
    }
}
  • Result
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.0

Abstract method

Basic Definitions

  • If a class contains Abstract methods, the class must be abstract.
  • Any subclass must override the abstract method of its parent or declare itself abstract.

Example

  • Employee (abstract class - employee class)
public abstract class Employee {
   private String name;
   private String address;
   private int number;
   
   public abstract double computePay();
   
   //Remaining code
}
  • Main points
    • If a class contains Abstract methods, the class must be abstract.
    • Any subclass must override the abstract method of its parent or declare itself abstract.

Subclasses that inherit abstract methods must override the method.Otherwise, the subclass must also be declared abstract.Ultimately, the abstract method must be implemented by subclasses, otherwise, objects cannot be instantiated from the original parent to the final subclass.

  • Salary (inherits abstract class - salary class)
    If the Salary class inherits the Employee class, it must implement the computePay() method:
// If the Salary class inherits the Employee class, it must implement the computePay() method:
public class Salary extends Employee {
   private double salary; // Annual salary
  
   public double computePay() {
      System.out.println("Computing salary pay for " + getName());
      return salary/52;
   }
 
   //Remaining code
}

summary

  • 1. Abstract classes cannot be instantiated. If instantiated, errors will occur and compilation will fail.Only non-abstract subclasses of abstract classes can create objects.
  • 2. Abstract classes do not necessarily contain Abstract methods, but classes with abstract methods must be abstract.
  • 3. An abstract method in an abstract class is only a declaration, does not contain a method body, or does not give a specific implementation of the method, that is, the specific function of the method.
  • 4. Construction methods, class methods (methods decorated with static) cannot be declared abstract.
  • 5. A subclass of an abstract class must give a concrete implementation of the abstract method in the abstract class unless it is also an abstract class.

Topics: Java