Abstract classes and abstract methods for Java (note see How to invoke non-abstract methods in abstract classes)

Source: Internet
Author: User

An abstract class is a class that cannot be instantiated with the new method, that is, a class that does not have a specific instance object. Abstract classes are a bit like "templates" that are designed to create and modify new classes based on their format. An object cannot be created directly by an abstract class, only a new subclass can be derived from an abstract class, and the object is created by its subclasses. When a class is declared as an abstract class, precede the class with the modifier abstract.

Member methods in an abstract class can include both generic and abstract methods. The abstract method is an abstract modified method that declares only the returned data type, the method name, and the required parameters, without the method body, which means that the abstract method needs to be declared without implementation. When a method is an abstract method, it means that the method must be overridden by the method of the quilt class, otherwise the method of its subclass is still abstract, and the subclass must be abstracted, which is declared as abstract.

Abstract classes do not necessarily contain abstract methods, but classes that contain abstract methods must be declared as abstract classes. The abstract class itself does not have the actual functionality and can only be used to derive its subclasses. an abstract class can contain a constructor method, but a construction method cannot be declared abstract.

Call the methods in the abstract class (abstract and non-abstract methods), and if the method is static, abstract the class directly. method, if it is non-static, you must require an inherited non-abstract class, and then use an instance of this non-abstract class to invoke the method.

Abstract classes can implement interfaces, the methods in an interface can not be implemented in an abstract class, when a subclass inherits an abstract class, and the subclass is not an abstract class, the subclass needs to implement the abstract methods in the abstract class and the methods in the interface

Abstract classes cannot be decorated with final, that is, a class cannot be both a final class and an abstract class.

Summary: Abstract cannot be used with private, static, final, native side-by-side modification of the same method.

Note: After the subclass inherits the parent class method, the subclass object executes the method, so long as the subclass overrides the subclass and does not execute the parent class.

Abstract classes and abstract methods for Java (note see How to invoke non-abstract methods in abstract classes)

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.