The meaning of Java interfaces and abstract classes and interfaces

Source: Internet
Author: User
Tags java web

Interface interface:

1. There is no variable in the interface, the member is defined by any means, and public static final is the same even if it is not explicitly declared.

2. All interface methods imply public abstract even if they are not explicitly declared. (and therefore cannot use protected and private adornments, not modifiers or default permissions)

3. Interfaces can inherit other interfaces but cannot inherit classes, cannot design methods in parent interfaces, and cannot implement other interfaces.

4. Any class can reference an interface and implement its methods in a self-class implementation.

Abstract class:

1. Variables and methods can define permissions, just like normal classes.

2. An abstract class can have only one inherited parent class but can implement many interfaces as well as normal classes.

3. Subclasses can inherit the same abstract class as the normal class. In other words, abstract classes are just ordinary classes that can use abstract methods.

See here, believe that everyone is thinking, since the interface itself can not achieve any function and can only provide some constants, then what is the meaning of the interface?

I think the interface is more like the upper layer of the document, in the Java Web Middle-tier development, will often provide an interface-to implement such a development model, in the case of the interface class does not change, arbitrary changes in the implementation of the other layers have no effect, as long as the interface does not gerze other layers need not change, The interface acts more like an API document. This reduces the coupling of the application and enhances the scalability of the program.

When we need to check which interfaces our objects use, which classes we inherit, we can check with instanceof, instanceof looks like a function name, but it is actually an operator, and it is used in a similar way:

Son son=new son ();

if (son isntanceof Father) {

}

The symbol to the left is an object, the right is a class name, you can check whether there is an inheritance relationship and implement implementation relationship, Chinese translation can be: can be seen as.

Using this operator, we can better organize our inherited polymorphic structure, or we can use some polymorphic functions better.

The meaning of Java interfaces and abstract classes and interfaces

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.