Simple understanding of Java abstract class _java

Source: Internet
Author: User
Tags ming

In the Top-down inheritance hierarchy, classes at the top are more versatile and perhaps even more abstract. In some ways, the ancestor class is more generic, it contains only the most basic members, and people only use it as a base class for deriving other classes, not for creating objects. Even, you can just give the definition of the method and not implement it, and the subclass will implement it in terms of specific requirements.

This method, which gives only the method definition and does not implement it, is called an abstract method, and the abstract method has no method body, and there is no "{}" in the expression of the code. A class that contains one or more abstract methods must also be declared as an abstract class.

Abstract methods and abstract classes are represented using the abstract modifier.

Abstract classes can contain specific variables and specific methods in addition to abstract methods. A class can be declared as an abstract class, even if it does not contain abstract methods, to prevent instantiation.

Abstract classes cannot be instantiated, and abstract methods must be implemented in subclasses. Take a look at the following code:

Import static java.lang.system.*;
Public final class demo{public
  static void Main (string[] args) {
    Teacher t = new Teacher ();
    T.setname ("Wang Ming");
    T.work ();
    
    Driver d = new Driver ();
    D.setname ("Xiao Chen");
    D.work ();
  }
Defines an abstract class abstract classes
people{
  private String name;//instance variable
  
  //Common setter and Getter methods public
  void S Etname (String name) {
    this.name = name;
  }
  Public String GetName () {return
    this.name;
  }
  
  Abstract method public
  abstract void work ();
}
Class Teacher extends people{
  //must implement this method public
  void work () {
    out.println ("My name is" + this.getname () + ", I am lecturing, please do not gaze around ... ");
  }
Class Driver extends people{
  //must implement this method public
  void work () {
    out.println ("My name is" + this.getname () + ", I'm driving, I can't answer the phone ... ");
  }

Run Result:

My name is Wang Ming, I am lecturing, please do not gaze
around ... My name is Xiao Chen, I am driving, can not answer the phone ...

A few notes on abstract classes:
Abstract classes cannot be used directly, you must implement abstract classes with subclasses, and then use instances of their subclasses. However, you can create a variable whose type is an abstract class and point it to an instance of a specific subclass, that is, you can use an abstract class to act as a formal parameter, and actually implement a class as an argument, a polymorphic application.
Cannot have an abstract constructor or an abstract static method.


A class becomes an abstract class in the following situations:
When one or more of the methods of a class is an abstract method;
When a class is a subclass of an abstract class and cannot provide any implementation details or method principals for any abstract method;
When a class implements an interface and cannot provide implementation details or method principals for any abstract method; Note:
This is the case where the next class will be an abstract class, without saying that the abstract class is bound to have these situations.
A typical error: An abstract class must contain an abstract method. But conversely, "classes that contain abstract methods must be abstract classes" are correct.
In fact, an abstract class can be a completely normal implementation of a class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.