Re-learning Java (3): Abstract classes and interfaces

Source: Internet
Author: User

first, abstract class

1. Abstract class can not be instantiated, instantiation to be given to subclasses to complete, it only needs a subclass to complete

2. Abstract methods must be overridden by subclasses

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

4. Abstract classes can contain specific methods, or they may not contain

5. Abstract methods in subclasses cannot be the same as the parent class

6.abstract cannot be associated withfinal modifies one class at a time

7.abstract cannot be associated withPrivate,Static,Final orNative side-by-side modification of the same method

second, the interface

An interface is more abstract than an abstract class."Class", the interface itself is not a class. It cannot be directlynew, such asnew Runnable (), can onlynew out of its real
present class.

Java is single-inheritance. A class can have only one parent class, and one may implement multiple interfaces.

Note the point:

1. All methods of the interface are automatically declared asPublic , and can only be declared asPublic .

2. The variables in the interface are automatically declared asPublic static final, which can be directly accessed directly from the class name.

3. There is no way to implement in the interface.

4. A non-abstract class implementation interface must implement all methods of the interface, the abstract class can not be implemented.

5. Not availableThe new operator modifies the interface. You can declare a variable, but the variable must point to an object that implements the class for that interface. can be usedinstanceof
detects whether a class implements an interface. For example:if (object instanceof comparable) {...}

6. To avoid duplication of interface naming when implementing multiple interfaces.

Three, the difference between abstract class and interface

1. The grammatical

Abstract classes can have their own arbitrary range of member data, and can have abstract or non-abstract methods. The interface can only have its own public static final modified

Only the method declaration. To some extent, an interface is a specialized form of an abstract class.

2.MORE

A. The level of abstraction. An abstract class is an abstraction of a class (including properties, behaviors ), and an interface is an abstraction of the behavior.

B. Abstract classes embody an inherited relationship, in essence, the same concept described by the abstract class and its subclasses, which is a more detailed description of the abstract class.

And the interface does not have this requirement, the interface is just the abstraction of all the same behavior, and embodies the contract of the behavior.

c. In general, abstract classes are abstracted from subclasses, while interfaces are not.

Re-learning Java (3): 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.