s2/java/04-interface

Source: Internet
Author: User
Tags constant definition

Interface definition syntax:[ modifier ] Interface Interface name extends parent interface 1, parent interface 2,..... {

constant Definition

method Definition

}

class implements the syntax of the interface: Class name extends parent class name implements interface 1, Interface 2,... {

class member

}

The naming conventions for interfaces are the same as classes. If the modifier is public, the interface is visible throughout the project, and if the modifier is omitted, the interface is visible only in the current package.

A constant can be defined in an interface, and a variable cannot be defined. The properties in the interface are automatically modified with public static final , that is, the properties in the interface are global static constants. Specifies the initial value when the constants in the interface must be redefined.

public static final int pi=3.14;

int pi=3.14; // in the mouth, these two definition statements effect exactly the same

Int PI; // Error! The initial value must be specified in the pretext, and there will be a default value in the class

All methods in an interface are abstract methods. The methods in the interface are automatically modified with public , that is, there is only a global abstract method in the interface.

Like abstract classes, interfaces cannot be instantiated, and there is no way to construct them in an interface.

An interface can implement an inheritance relationship through extends, an interface can inherit multiple interfaces, but the interface cannot inherit classes.

An implementation class for an interface must implement all the methods of an interface, otherwise it must be defined as an abstract class.

A class can have only one direct parent class, but multiple interfaces can be implemented through implements . When a class implements more than one interface while inheriting the parent class, theextends keyword must precede the implements keyword.

The difference between an interface and an abstract class is that abstract classes utilize code reuse and interfaces facilitate code maintenance.

Interface-oriented programming is advocated in object-oriented programming rather than implementation-oriented programming.

syntax for defining an interface in C # : [ modifier ] Interface Interface name : Parent Interface 1, parent interface 2,..... {

Property Definition

method Definition

}

syntax for implementing an interface in C #: Class name : Parent class name , interface 1, Interface 2,... {       }

Interfaces can be inherited by a colon ":" , an interface can inherit multiple interfaces, but interfaces cannot inherit classes. A class can inherit only one parent class, but it is possible to implement multiple interfaces by using the colon ":" to inherit the class and implement the interface.

An interface defines 0 or more members, mainly methods, properties, and indexers. The interface cannot contain constants, variables, or construction methods, nor can it contain any static members.

The member access rights in the interface IE are public , and it is illegal to display any modifiers when defining the interface.

By convention, the name of the interface in C # begins with the capital letter "I" .

In the Java and C # languages, interfaces have the following differences.

The Java Total interface inherits the parent interface through extends, the class implements the interface through implements, and C # uses the colon ":" to implement these two functions.

member variables (attributes) in the Java interface are constants that are automatically modified with public static final, and theC # interface does not allow member variables, but can have properties.

the properties and methods in the Java interface can be decorated with public , which is public by default in C # , but does not allow the display to use the public adornment.

static constants and methods can be defined in the Java interface, and no static members are allowed in the C # interface.

s2/java/04-interface

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.