Java records -16-abstract classes and interfaces

Source: Internet
Author: User

Java abstraction classes and interfaces (abstract class and interface)

  1. Classes decorated with the abstract keyword are called abstract classes. Abstract classes cannot be materialized, that is, you cannot new an object (instance) of an abstract class.

  2. Abstract method, the method that is decorated with the abstract keyword is called an abstract method. Abstract methods need to be defined in an abstract class. In contrast to abstract methods, the previously defined methods are called concrete methods (with declarations, implementations).

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

  4. If a class is an abstract class, the class can contain concrete methods (declarations, implementations).

  5. If a class contains abstract methods, then the class must be declared abstract class, that is, the class must be an abstraction, and conversely, if a class is an abstract class, the class can contain either an abstract method or a concrete method.

  6. In either case, this class cannot be instantiated as long as the class is an abstract class.

  7. In the case where the subclass inherits the parent class (the parent class is an abstract class), then the subclass must implement all the abstract methods defined in the parent class, otherwise the subclass needs to be declared as an abstract class.

  8. Example: various sorts, calculate area

  9. Interface (interface): An interface is equivalent to class, and all methods in an interface are abstract methods. When you declare a method in an interface, you can use the abstract keyword or you can not use it. Typically, the abstract keyword is omitted.

  10. Interfaces can be thought of as special abstract classes (abstract classes can have specific methods or abstract methods, and interfaces can only have abstract methods and no concrete methods).

  11. class can implement interfaces. The implementation uses the keyword implements representation, which represents a class that implements an interface.

  12. When a class implements an interface, the class must implement all the methods declared in the interface. If the class is an abstract class, then you do not need to implement the methods in the interface.

  13. Java is single-inheritance, meaning that a class can only be made up of a single parent class, a class may implement multiple interfaces, and multiple interfaces are separated by commas.

  14. Polymorphic: A so-called polymorphic, that is, a reference to a parent type can point to an object of a subtype, or a reference to an interface type can point to an instance of the class that implements the interface. The forced type conversion between the interface and the class that implements the interface is exactly the same as the coercion type conversion between the parent class and the child class.

Java records -16-abstract classes and interfaces

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.