Java Abstract classes and abstract methods

Source: Internet
Author: User

The following describes the association between abstract classes and abstract methods:

Abstract classes are classes that cannot be instantiated using the new method, that is, classes without specific instance objects.

Abstract classes are similar to "templates" to create and modify new classes based on their formats. Objects cannot be directly created by abstract classes. They can only be derived from abstract classes to create objects. When a class is declared as an abstract class, the modifier abstract must be added before the class.

The member methods in the abstract class can include general methods and abstract methods. Abstract methods are modified with abstract. This method only declares the returned data type, method name, and required parameters. There is no method body. That is to say, abstract methods only need to be declared without implementation. When a method is an abstract method, it means that the method must be overwritten by the method of the quilt class. Otherwise, the method of its subclass is still abstract, and The subclass must be abstract, the statement is abstract.

Abstract classes do not necessarily contain abstract methods, but classes that contain abstract methods must be declared as abstract classes. Abstract classes do not have actual functions and can only be used to derive their subclasses. Abstract classes can contain constructor methods, but they cannot be declared as abstract.

Call abstract methods and non-abstract methods in abstract classes. If the methods are static, they are directly abstract classes. The method is fine. If it is not static, an inherited non-abstract class must be required, and then the non-abstract class instance is used to call the method.

Abstract classes can implement interfaces. Methods in interfaces can not be implemented in abstract classes. When a subclass inherits an abstract class and the subclass is not an abstract class, the subclass must implement both the abstract methods in the abstract class and the methods in the interface.

Abstract classes cannot be modified using final, that is, a class cannot be both a final class and an abstract class.

Conclusion: abstract cannot be used in parallel with private, static, final, or native to modify the same method.

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.