Why design abstract classes? Interface and abstract class

Source: Internet
Author: User

An interface is a special abstract class, which is usually used by inheritance.

Why design abstract classes? For inheritance, common classes can also be used for inheritance. However, in reality, there are some classes that do not make sense to instantiate them. For example, animal, we do not instantiate an animal object, but instantiate a specific animal object such as bird and tiger. In this way, some methods can be declared in the class and are not implemented, this method is an abstract method.

Abstract classes can have common methods. abstract classes can have no abstract methods. However, classes with abstract methods must be abstract classes.
This shows the role of abstract classes:
1 is used to include some non-implementation methods.
2. It is used to determine the role of this class (can it be instantiated)
3. After inheritance, the abstract method must be rewritten.

The more pure abstract classes are interfaces, indicating that they all look like this and have the following features:
1. Full abstract class without any implementation
2 can be used for multi-Inheritance
3 contains attributes and is automatically static final.
4. The method is automatically public.
5. It is used for full decoupling, such as for policy mode and factory.

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.