Object-Oriented ———— interface

Source: Internet
Author: User

An interface is another way to define a data type. It is very similar to the class.

The same: There are member variables and member methods

can also form an inheritance relationship

Differences: Properties in an interface are constants (final)

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

Reasons for introducing interfaces: Java only supports single-inheritance, and each class can have only one superclass. However, in practical applications, multiple inheritance is sometimes required--using interfaces, a class can implement multiple interfaces.

Definition of the interface

[Public] (indicates that it can be used by classes or interfaces in different packages, with in-package accessibility by default) interface (interface keyword represents interface) interface name (any valid identifier) [Extends (interface supports multiple inheritance, Multiple parent interfaces separated by commas) parent Interface 1, parent interface 2,...] {

[Public] [Static] [Final] Data Type property name = constant value;

[Public abstract] Returns a value type method name ([formal parameter list]);

The properties of an interface must be modified with public static final, which is the system default, which can be omitted partially or omitted, but is generally written final.
int max_size = 50; Equivalent to
public static final int max_size = 50;

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

Example 5.19 the definition of an interface.

public interface Figure {

Final double pi=3.14;

abstract void area ();

}

Implementing interfaces

An object cannot be created directly after an interface definition, and objects must be created by the class after the interface has been implemented. Each class can inherit only one base class, but it can implement multiple interfaces. The class definition form is as follows:

[Public] class name extends base class implements (multiple interfaces can be implemented, multiple interfaces separated by commas

) 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. )

public class Circle implements figure{

Double radius;

Public Circle (double R) {

Radius=r;

}

public void area () {

System.out.println ("area of the Circle =" +pi*radius*radius);

}

}

Attention:

When implementing an abstract method in an interface, the method header must be exactly the same as the method header in the interface definition except for the keyword abstract, and the public modifier cannot be omitted.

If a class implements more than one interface, you must override all of the methods in those interfaces.

Interfaces are not classes, and you cannot instantiate interfaces with new, but you can declare interface variables. An interface variable can point to an object that implements the class of that interface, for example,
Shape s=new shape (); Wrong
Shape s=new Circle (); Right

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

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

For example, class Rectangle implements shape{}

Shape s;

S=new Circle (); S.area ();

S=new Rectangle (); S.area ();

Emphasis: An interface defines a contract in which the class that implements the interface must abide by its contract. Interfaces are best suited for providing common functionality for unrelated classes, and using the methods provided by interfaces, programs can handle objects of these completely different classes in a polymorphic manner.

Inheritance of interfaces

Interfaces have multiple inheritance characteristics, that is, an interface can have multiple parent interfaces. The new subinterface inherits all the methods and constants of all the parent interfaces. The format for interface inheritance is:

[Public] Interface sub-interface extends parent interface 1, parent interface 2, ... {

static properties;

abstract methods;

}

Interfaces and abstract classes

Interfaces are like abstract classes, and they all have the following characteristics:

Neither interfaces nor abstract classes can 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

Interface is a specification, similar to the whole system of "general", it has developed the system of the modules should be followed by the standard. therefore interfaces in a system should not change frequently

Abstract class, as a common parent of multiple subclasses, embodies the template-style design. Abstract classes can be used as intermediate products in the system implementation process, this intermediate product has been implemented 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.

Different usage

(1) The interface can contain only abstract methods

(2) The static method cannot be defined in the interface

(3) Only static constant properties can be defined in the interface

(4) The interface does not contain a construction method

(5) The initialization block cannot be included in the interface

(6) A class can implement multiple interfaces

(1) Abstract classes can contain common methods.

(2) Abstract classes can define static methods

(3) An abstract class contains both normal properties and static constant properties

(4) The constructor method in the abstract class is not used to create the object, it is to let the child class call

(5) An abstract class can contain initialization blocks

(6) One can inherit only one parent class

Object-Oriented ———— 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.