Understanding of Java Abstract Classes

Source: Internet
Author: User

1. Basic Concepts

Classes that are decorated with abstract are abstract classes. If there are methods in the class that are of the abstract type, then this class must be of the abstract type, that is, the modifiers of this class are definitely abstract (that is, the class with the abstract method is an abstraction class). Abstract classes are incomplete and can only be used as base classes and cannot be instantiated.

2. Comparing with classes

Abstract classes cannot be instantiated, and using the new operator directly on abstract classes can cause compilation errors.

Allows an abstract class to contain abstract methods or variables.

Abstract classes cannot be private.

3. And interface comparison

Different points:

Abstract classes can have implemented methods, interfaces cannot have.

A class can implement an infinite number of interfaces, but only one abstract class (or other) can be inherited, and the abstract class-derived class throws the interface, which concludes that the interface is used to solve the problem of multiple inheritance.

Abstract classes can exist non-abstract methods, interfaces can not, and the method inside it is only a declaration must be public to decorate the method without concrete implementation.

member variables in an abstract class can be decorated with different modifiers, and the member variables in the interface can default to static constants (static final).

An abstract class is an abstraction of an object, whereas an interface is a behavior specification.

Similar points:

are not instantiated.

Can contain method declarations that are not implemented.

Derived classes must implement methods that are not implemented, where abstract classes are abstract methods and interfaces are all members.

Tip:1. The method of implements declaration cannot be modified by using private. 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. 3. Classes that implement abstract classes and interfaces must implement all of these methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface. 4. 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. 5. The methods in the interface are default to the Public,abstract type.6. Interfaces can inherit interfaces (using extends statements); Abstract classes can implement interfaces; abstract classes can inherit entity classes. Abstract classes do not need to implement all the methods of an interface. 7. Interesting place: The abstract class inherits the interface, and then the entity class inherits the abstract class, discovering the method that does not need to implement the first interface definition. Thus, the method of the interface definition becomes the existence of an abstract method when the entity class is implemented. But according to TIP5, that's the way it is.

Understanding of Java Abstract Classes

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.