Abstract class and interface learning Summary

Source: Internet
Author: User
Tags abstract definition
Abstract class: In some classes, certain functions cannot define specific behavior functions. Such functions are called abstract functions. classes that contain abstract functions are called abstract classes.

Abstract class features: abstract classes and abstract methods must be modified with abstract keywords.

Abstract class format: abstract class name {}

Format of the abstract method: public abstract void ()

Abstract classes do not necessarily contain abstract methods. classes with abstract methods must be abstract classes.

Abstract classes cannot be instantiated because they are not specific implementation classes.

Abstract classes need to be instantiated by specific subclasses in the form of polymorphism.

The subclass of an abstract class is either an abstract class, or all methods in the abstract class must be rewritten and implemented.

Abstract classes have constructor methods, but cannot be instantiated. constructor is used to initialize the parent class data when the child class accesses the parent class data.

The access permission of the abstract method must be public.

Abstract class member features:

Member variable: it can be a variable or constant.

Constructor: constructor is available, but cannot be instantiated.

Member method: it can be an abstract method or a non-abstract member method.

Abstract method: Forces subclass to do its own thing

Non-abstract method: It is inherited by sub-classes to improve code reusability.

Abstract can include static variables, constants, and static methods.

The empty implementation of a method is not equivalent to the abstract method.
Polymorphism is implemented to improve program scalability.

In software development, an empty class is usually defined as the base class to facilitate the organization and management of the Code architecture.

Software analysis process: from specific functions to abstract definitions.

Software implementation process: from abstract definition to specific functions.


Small issues with abstract classes

1. abstract classes cannot be modified using final. Abstract methods cannot be modified using final.

2. abstract methods cannot be modified using private.

3. abstract methods cannot be modified using static.


Interface: this interface is used to present abstract descriptions of some special functions. It is handed over to the class implementing the interface to complete the methods in the interface.

Interface features:

The interface is represented by the keyword interface.

Format: interface name {}

 

Class implementation interface method calss class name implements interface name {}

 

Interface instantiation: The interface cannot be directly instantiated. The specific subclass needs to be instantiated according to the polymorphism mode. Interfaces are also a form of polymorphism.

 

The class that implements the interface is either an abstract class, or rewrite and implement all the abstract methods in the interface.

 

The variables defined in the interface are all public static by default.

The methods defined in the interface are all modified using public abstract by default.

Internal class defined in the interface. The interface is modified with public static by default.

Interfaces can inherit interfaces.

The interface can have no members and serve as a certain identifier.

 

What is the difference between an interface and an abstract class?

The interface cannot have non-abstract methods and non-static variables.

 

The abstract class implementation of the interface has no practical significance.

 

Three manifestations of polymorphism: The polymorphism of a specific class of polymorphism abstract class of polymorphism interface Polymorphism



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Abstract class and interface learning Summary

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.