Home > Java > JavaBase > body text

What is java abstract class

(*-*)浩
Release: 2019-11-11 11:48:30
Original
3775 people have browsed it

In the Java language, when the abstract keyword is used to modify a class, the class is called an abstract class. An abstract class is a collection of public attributes of all its subclasses and a class that contains one or more abstract methods.

What is java abstract class

Abstract class can be seen as a further abstraction of the class. In the object-oriented field, abstract classes are mainly used for type hiding.

Abstract method (Recommended learning: java course)

Abstract classes can contain general methods and abstract methods . The definition of an abstract method is different from that of a general method. An abstract method is directly followed by a semicolon after the method header, while a general method contains a method body enclosed in curly braces. All abstract methods must exist in abstract classes.

Methods defined in abstract classes are called abstract methods. These methods only have the declaration of the method header, and a semicolon is used to replace the definition of the method body, that is, only the interface form of the member method is defined without the concrete operate.

Only the redefinition of abstract member methods by a derived class can truly implement operations related to the derived class.

After each subclass inherits the abstract method of the parent class, it redefines it with different statements and method bodies, forming several subclasses with the same name, the same return value, the same parameter list, and the same purpose but There are certain differences in the specific implementation methods.

The purpose of defining abstract methods in abstract classes is to implement an interface, that is, all subclasses present a method with the same name to the outside world.

Use abstract class to define abstract classes in Java language. 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, 7 member methods and 1 constructor.

But it needs to inherit and override abstract methods and cannot be instantiated directly.

The above is the detailed content of What is java abstract class. 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!