Java Experience---interface __java

Source: Internet
Author: User
Tags class definition inheritance

1. In Java, classes and classes can only be single inheritance, not multiple inheritance. Multiple Inheritance Disadvantage: (1) If you have a variable with the same name in multiple parent classes that inherit from a subclass, a subclass is ambiguous when referencing the variable, cannot determine which parent class should be used, (2) inherits more than one method, and the method is not overridden in the subclass, then the method is ambiguous when invoked. Java provides an interface that implements the advantages of multiple inheritance through the functionality of the interface and rejects its drawbacks.

separating the specification from the implementation is exactly the benefit of the interface, allowing for interface coupling between components of the software system, and is a coupling (connection) design.

this interface-oriented coupling should also be adopted among the modules of the software system, so as to minimize the coupling between the modules and provide better scalability and maintainability for the system.

2. Unlike the definition of a class, the definition of an interface no longer uses class, but rather uses interface, from a point of view, the interface can be viewed as a special class, so a Java source file can have a maximum of only one public interface, if a public interface is defined, The source file must have the same name as the main name of the interface.

basic Syntax Format:

[public] Interface interface Name [extends parent 1, parent Interface 2,...] {//interface supports multiple inheritance, separated by commas between multiple parent interfaces

[public][static][final] Data Type property name = constant value;

[public abstract] Returns the value type method name ([formal parameter list]);

1. The properties of the interface must be in public static final modification, is the system default, can be omitted or omitted partially, but generally write final.

int max_size = 50; Equivalent to

Public static final int max_size = m;

2. The method of the interface defaults to Publicabstract, generally does not write modifiers, can be omitted--the methods in the interface are abstract methods.

3. The member variables in the interface are common, static, and final constants. The methods in the interface are public, abstract, with only method signatures, no method bodies.

3. An interface is another way to define a data type. It's very similar to class.

U : All have member variables and member methods

You can also form an inheritance relationship

The difference: the properties in the interface are constant (final)

the method in the interface is an abstract method (no method body)

the reason for introducing an interface: Java only supports single inheritance, each class can have only one superclass. But in practical applications, multiple inheritance is sometimes required--using interfaces, a class can implement multiple interfaces.

4. When an interface is defined, it cannot be created directly, and the object of the class must be created after the class implements the interface. Each class can inherit only one base class, but multiple interfaces may be implemented. Interfaces cannot display inheriting any classes, but reference variables of all interface types can be assigned directly to reference variables of type object. The compiler knows that any Java object must be an instance of object or its subclasses. The class definition form is as follows:

[public] class name extends base class implements interface 1,... {//Class body}

Note: The class implements the interface, inheriting all member variables and member methods in the interface. Because the methods in the interface are abstract, the classes that implement the interfaces must override these methods.

5. Note:

when you implement an abstract method in an interface, the method header must be exactly the same as the method header in the interface definition, in addition to removing the keyword abstract, and the public modifier cannot be omitted.

if your class implements multiple interfaces, you must override all of the methods in those interfaces.

U interface is not a class, you cannot instantiate an interface with new, but you can declare an interface variable. An interface variable can point to an object of the class that implements the interface, for example,
Shape s=new shape (); Wrong
Shape s=new Circle (); Right

U can use instanceof to determine whether an object implements an interface.

Although you can use an interface variable to reference an object that implements an interface class, this reference can only refer to the members of the interface, or a compilation error occurs.

emphasis: An interface defines a contract, and the class that implements the interface must comply with its agreement. Interfaces are best suited for providing generic functionality for unrelated classes, and using the methods provided by the interface, programs can handle objects of these completely different classes in a polymorphic manner.

6. Interfaces and abstract classes

Μ interfaces are similar to abstract classes, and they all have the following characteristics:

both interfaces and abstract classes cannot be instantiated, they are at the top of the inheritance tree and are implemented and inherited by other classes.

both interfaces and abstract classes can contain abstract methods, and ordinary subclasses that implement interfaces or inherit abstract classes must implement these abstract methods.

Μ Difference

different design purposes

interfaces embody a specification, similar to the "General layout" of the entire system, which set the standards that each module of the system should follow. Therefore, interfaces in a system should not be changed frequently

abstract classes, as common parent classes of multiple subclasses, embody the template design. Abstract class can be used as a system implementation of the intermediate product, this intermediate product has implemented a part of the function, but this product can not be regarded as the final product, must be further improved, this improvement may have several different ways.

U use different

(1) An interface can contain only abstract methods; an abstract class may contain a common method

(2) A static method cannot be defined in an interface; an abstract class can define a static method

(3) Only static constants can be defined in the interface, and the abstract class contains both normal attributes and static constant properties

(4) The interface does not contain a constructor method; The constructor method in the abstract class is not used to create the object, so the subclass calls the

(5) An interface cannot contain an initialization block; an abstract class can contain an initialization block

(6) A class can implement multiple interfaces; one can inherit only one parent class, including an abstract class.

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.