(i) Java Foundation consolidation

Source: Internet
Author: User

Abstract class:principles of abstract class adherence:(1) The abstract keyword can only be decorated with classes and methods and cannot be decorated with fields. (2) An abstract class cannot be instantiated (an instance of an object cannot be created with the New keyword) and can only be inherited. (3) Abstract classes can contain properties, methods, constructor methods, initialization blocks, inner classes, enumeration classes, and ordinary classes, the ordinary method must be implemented,A variable can be initialized or uninitialized but cannot be initialized after it is re-assigned in an abstract class or manipulated (the variable can only be changed in a subclass). (4) abstract methods in abstract classes (methods that add the abstract keyword) cannot be implemented. (5) A class containing an abstract method must be defined as an abstract class.  extension: The difference between abstract classes and interfaces, make a summary:

(1) The interface is public, it cannot have private methods or variables, is used to let others use, and abstract classes can have private methods or private variables.

(2) Abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. However, a class can implement multiple interface and implement multiple inheritance. The interface also has the function of identifying (there are no methods, such as remote interfaces) and data sharing (the variables inside are all constants).

(3) The abstract class can have its own data members, but also can have non-ABSTARCT member methods, and in interface, can only have static can not be modified data members (that is, must be static final, but in Data members are not generally defined in interface, and all member methods are the public abstract type by default.

(4) Abstract class and interface reflect the different design concepts. In fact, abstract class represents the "is-a" relationship, interface represents the "has-a" relationship.

(5) The implementation of the interface must be implemented in all the methods defined in the interface, and the implementation of the abstract class can selectively rewrite the required method, the general application, the top-level interface, and then the abstract class implementation interface, finally to the concrete class implementation. Abstract classes can have non-abstract methods. There is no implementation method in the interface.

(6) The variable defined in the interface is the public static final type by default, and must be given its initial value, so the implementation class cannot be redefined or changed. Variables in abstract classes are friendly by default, their values can be redefined in subclasses, or they can be re-assigned in subclasses

(i) Java Foundation consolidation

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.