Home > Java > javaTutorial > body text

Java abstract class definition and example code sharing of abstract methods

黄舟
Release: 2017-04-15 09:08:10
Original
2420 people have browsed it

This article mainly introduces the relevant information about the difference between java Abstract class and Interface. Friends in need can refer to it...

Object-orientedIn the concept, all objects are described by classes, but conversely, not all classes are used to describe objects. If a class does not contain enough information to describe A concrete object, such a class is an abstract class.

Except that abstract classes cannot instantiate objects, the other functions of the class still exist. Member variables, member methods and constructors are accessed in the same way as ordinary classes.

Since abstract classes cannot instantiate objects, abstract classes must be inherited before they can be used. For this reason, it is usually decided during the design stage whether to design an abstract class.

The parent class contains common methods of the subclass collection, but because the parent class itself is abstract, these methods cannot be used.

Abstract class

Use abstract class in Java language to define abstract classes. The following example:


/* 文件名 : 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;
  }
}
Copy after login

Notice that the Employee class is no different. Although the class is an abstract class, it still has 3 member variables and 7 members. method and 1 constructor. Now if you try the following example:

/* 文件名 : AbstractDemo.java */
public class AbstractDemo
{
  public static void main(String [] args)
  {
   /* 以下是不允许的,会引发错误 */
   Employee e = new Employee("George W.", "Houston, TX", 43);
   System.out.println("\n Call mailCheck using Employee reference--");
   e.mailCheck();
  }
}
Copy after login

When you try to compile the AbstractDemo class, the following error will be generated:

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

Inheriting abstract class

We can inherit the Employee class through the general method:

/* 文件名 : 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;
  }
}
Copy after login

Although we cannot instantiate an object of the Employee class, if we instantiate a Salary class object, the object will inherit 3 from the Employee class Member variables and 7 member methods.

/* 文件名 : 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();
  }
}
Copy after login

The results of compiling and running 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.
Copy after login

Abstract method

If you want to design such a class, the class contains a special member method, the specific implementation of the method is determined by its subclass, then you can declare the method as an abstract method in the parent class.

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

The abstract method is not defined, and the method name is directly 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();  
  //其余代码
}
Copy after login

Declaring an abstract method will cause the following two results:

If a class contains an abstract method, then the class must be an abstract class.

Any subclass must override the abstract method of the parent class, or declare itself as an abstract class.

Subclasses that inherit abstract methods must overload this method. Otherwise, the subclass must also be declared abstract. Eventually, a subclass must implement the abstract method, otherwise, neither the initial 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:

/* 文件名 : 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;
  }
  //其余代码
}
Copy after login

The above is the detailed content of Java abstract class definition and example code sharing of abstract methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!