03day_java Object-oriented

Source: Internet
Author: User

First, the interface

1. Interface concept

An interface is a collection of functions, which can be regarded as a data type and a more abstract class than an abstract class;

The interface value description should have the method, and there is no specific implementation, the implementation of the interface is implemented by the class (equivalent to the subclass of the interface) to complete. In this way, the definition and realization of function are separated, and the program design is optimized.

All things have function, that is, everything has an interface.

2, the definition of the interface

The interface keyword is required when the interface definition is used;

Defines that the interface is still in a . Java file that, when declared, will still produce a . class file when it is compiled with the interface keyword;

Define the format:

Public interface Interface Name {

Abstract Method 1;

Abstract Method 2;

Abstract Method 3;

}

Using interface instead of the original class, the other steps are the same as the definition class;

The methods in the interface are the abstract methods of public access, and the modifiers are publicly written or not written;

A normal member variable cannot be defined in an interface and must be defined as a constant, fixed format: public static final data type variable name = value.

Public permissions

Static can be called directly by the class name and can be called directly by the name of the interface.

Final Finally, the value of the fixed variable

3, class implementation interface

The relationship of a class to an interface is to implement the relationship, that is, the class implements the interface. The implemented action is similar to inheritance, except that the keyword is different, implemented using the Implements,class class implements interface {

overriding an abstract method in an interface

}

After the class implements the interface, the class will inherit the abstract method in the interface, at which point the class needs to rewrite the abstract method to complete the concrete logic;

interface defines the function, when the need for the function, you can let the class implementation of the interface, only declare that should have the method, is a function of the Declaration;

In the concrete implementation class, the method is implemented by overriding the method and implementing the function.

As a result, the Declaration and implementation of the function are separated by the above two actions (the class is the description of the real thing.) An interface is a collection of features).

4, the characteristics of the interface members

The variable can be defined in the interface, but the variable must have a fixed modifier, public static final (can not write or selectively write, but the default is fully written) so the variable in the interface is also called a constant, its amount can not be changed;

The method can be defined in the interface, and the method has a fixed modifier public abstract (can be written without writing or optional, but it is all written by default)

Interface is not available to create objects;

The implementation class must override all of the abstract methods in the interface to be instantiated, otherwise the implementation class is an abstract class.

5, multi-implementation in the interface

The most important embodiment of the interface: to solve the disadvantages of multiple inheritance, the multi-inheritance of this mechanism in Java through the implementation of multiple implementations;

Disadvantage: When multiple inheritance, when the same function in more than one parent class, the subclass invocation will produce uncertainty;

The core reason: the multi-inheriting parent class has the main body, which causes the runtime to not determine which principal content to run;

WORKAROUND: Because the functions in the interface are all abstract, there is no body method body, which is defined by the implementation class.

6. Class inheriting class implements interface simultaneously

A class has inherited a parent class, and it needs to extend additional functionality;

The presence of interfaces avoids the limitations of single inheritance. The parent class defines the basic function of a thing, and the extended functionality of things defined in an interface.

7. Multiple Inheritance of interfaces

If the same method exists in multiple interfaces, when several classes implement these interfaces, then the method in the interface is implemented, because the methods in the interface are abstract, and the uncertainty of the call does not occur after the subclass.

8, the idea of the interface

Benefits of the interface

(1) The appearance of the interface expands the function;

(2) The interface is actually exposed rules;

(3) The appearance of the interface reduces coupling, that is, the realization of decoupling between equipment and equipment.

(4) The appearance of the interface is convenient for later use and maintenance, one side is in the use of interfaces (such as computers), a party in the implementation of the interface (plugged in the socket device).

9. Differences between interfaces and abstract classes

Same point:

(1) are located at the top of the inheritance for implementation or inheritance by other classes;

(2) can not directly instantiate the object;

(3) all contain abstract methods, and their subclasses must overwrite these abstract methods.

Difference:

(1) Abstract classes provide implementations for some of the methods to avoid subclasses repeating these methods, providing reuse of code, and interfaces can only contain abstract methods;

(2) A class can inherit only one direct parent (possibly an abstract class), but may implement multiple interfaces (the interface compensates for Java's single inheritance);

(3) Abstract class is the content that should have in this thing, the inheritance system is a kind is. a relationship;

(4) The interface is an additional function in this thing, and the inheritance system is like. a relationship;

The choice of the two

Preferential selection of interface, as far as possible to use abstract classes;

You need to define the behavior of subclasses and then use abstract classes to provide generic functionality for subclasses.

Second, polymorphic

1. Overview of polymorphism

Polymorphism is the third major feature of object-oriented after encapsulation and inheritance.

Java in red polymorphic code embodied in a subclass object (Implementation class object) can either give the subclass (Implementation class object) reference variable assignment, but also to the subclass (Implementation class object) of the parent class (interface) variable assignment;

The final polymorphism manifests in the parent class application variable can point to the subclass object;

The precondition of polymorphism is that it is necessary to have the relationship of the parent class or the class to implement the interface relationship, otherwise the polymorphic can not be completed;

A subclass-overridden method is called when the method is called by the parent class after the abortion is used.

2, polymorphic definition and use of the format

(1) The reference variable of the parent class points to the child class object

Parent class type variable name =new subclass type ();

The variable name . Method name ();

(2) The format of an abstract class of polymorphic definitions

Abstract class variable name =new abstract class sub-class ();

(3) format of the interface polymorphism definition

Interface variable name =new interface implementation Class ();

Precautions:

The method of the same parent class is overridden by a different subclass, and when the method is called, the overridden method for each subclass is called;

When a variable name points to a different subclass object, different methods are called because each subclass overrides the contents of the parent class method.

3. Characteristics of member methods in polymorphic states

Member variables
(1) Compile time: The reference of the parent class there is no this variable, if there is, compile successfully, no failure;

(2) Operation period: The variable value of the parent class is running;

Simple to remember: Compile run all look at the parent class.

Member Methods

(1) Compile time: Refer to the parent class there is no this method, if there is, compile successfully, no failure;

(2) Operation period: The sub-class is the overriding method;

Simple to remember: Compile to look at the parent class, run to see the subclass.

4. instanceof Key Words

Determine whether an object belongs to a data type by using the instanceof keyword.

Use format:

Boolean b= object instanceof data type ();

5. Multi-State transformation

(1) Upward transformation

When a subclass object is assigned a reference to a parent class, it is an upward transformation, and polymorphism itself is the process of upward transformation.

Use format:

Parent class type variable name =new subclass type ();

such as: Person P=new Students ();

(2) Downward transformation

A child class object that has been transformed upward can use the format of coercion type conversion to convert the parent class reference to a subclass reference, which is a downward transformation. If you create a parent class object directly, you cannot change it down!

Use format:

Subclass type variable name = (subclass type) A variable of the parent class type;

such as: Student stu = (Student) p; The variable p actually points to the student object

03day_java Object-oriented

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.