Interface interface in Java

Source: Internet
Author: User
Tags instance method

About interfaces

The interface describes what functions the class that implements it has. Because Java is strongly typed, some operations must be constrained and tagged with interfaces. Interface as proof of the class's ability, it shows what the class that implements the interface can do.

Members in an interface

All members of the interface, by default, are public, and must be public, either method or field. Because the interface is to specify the API, if the implementation of the class does not expose the API in the interface, how can I call the API?

You can define methods and fields in an interface.

The field in the interface , the default (also must) is public static final. Must be initialized in the defined interface, because it is final. They will become static constant fields in the class

the method in the interface , which is the default (and must) for public abstract. Just define it in the interface and not implement it (let the class implement the interface). will become the public instance method in the class, and the class implementing the interface must implement the method defined in the interface.

The implements relationship between interfaces and classes

Interface is used to be implemented implements, otherwise the definition of the interface is meaningless. A class in Java can inherit only one parent class, but it is possible to implement multiple interfaces. In addition to abstract classes, the ordinary class must implement all the methods defined in the interface.

When a class implements an interface, the interface can be considered to be the parent class of the class that implements the interface. Using polymorphic features, we tend to choose interface-oriented programming rather than specific classes, which allows programmers to focus on the API rather than the internal implementation and provides greater flexibility.

 Public classTest { Public Static voidMain (string[] args) {shape[] shapes= {                            NewRectangle (5,6),            NewTriangle (3, 4, 5),            NewTriangle (6, 8, 10)            }; DoubleTotalarea =0; DoubleTotalcircle =0;  for(Shape s:shapes) {Totalarea+=S.getarea (); Totalcircle+=s.getcircle (); } System.out.println ("Totalarea:" +Totalarea); System.out.println ("Totalcircle:" +totalcircle); System.out.println (NewTriangle (3,4,5)instanceofShape);//true                    }}Interfaceshape{DoubleGetarea (); Doublegetcircle ();}classRectangleImplementsshape{Rectangle (intWinth) {width=W; Height=h; }        Private intwidth; Private intheight; @Override Public DoubleGetarea () {returnheight*width; } @Override Public Doublegetcircle () {return(height+width); }    }classTriangleImplementsshape{Triangle (intAintBintc) {E1=A; E2=b; E3=C; }        Private intE1; Private intE2; Private intE3; @Override Public DoubleGetarea () {Doublep = (E1+E2+E3)/2.0; DoubleArea= math.sqrt (p* (p-e1) * (p-e2) * (P-e3)); returnArea ; } @Override Public Doublegetcircle () {returne1+e2+E3; }    }

Abstract class to interface

Examples in the Java collection framework

The extends relationship between interfaces and interfaces

An interface can extend an interface that extends already exists, and if interface a extends interface B, a inherits all the members in B as if they were defined in a. An interface can extend multiple interfaces .

Interface shape{    double  getarea ();     Double getcircle ();} Interface color{    void setcolor (int  Color);     void GetColor ();} Interface extends Shape, color{    double  getwidth ();     Double getheight ();    }

Thus, if a class is to implement the rectangle interface, the above 6 methods must be implemented.

Summary of the interface

1, the interface cannot be new out of the instance. However, you can define a variable of the interface type to reference the object of the class that implements the interface. At this point the interface is equivalent to the parent class of the implementation class.

2, the method of the interface is not realized, but with a good point, instead of the function body.

3, the interface is a reference type in Java.

Interface interface in Java

Related Article

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.