Java defines the interface format:

Source: Internet
Author: User

[Public]interface Interface Name [extends parent interface list]

{

Static constants

[Public] [Static] [Final] Data type variable name = constant value;

Abstract methods

[Public] [Abstract] [Native] Returns the value type method name (parameter list);

}

Implement the interface format:

[Modifier] class class name [extends parent class name] [implements interface A, interface b,]

{

Class member variables and member methods;

Write the method body for all the methods in interface A, implement interface A;

Write the method body for all methods in interface B, implement interface B;

}

Instance:

This example defines the interface Areainterface, where there are static constant Pai and an abstract method area () to find areas. Class circle and class rectangle implement the Areainterface interface, that is, the abstract method area () in the interface is written to meet the respective requirements of the method body, respectively, the size of the circle and rectangle.

Program: Areainterface.java

1

2

3

4

5

Package Jiekou;

public interface areainterface{

Double Pai=math.pi;

Double area ();

}

Program: Circle.java

1

2

3

4

5

6

7

8

9

10

11

12

13

Package Jiekou;

public class Circle implements areainterface{

Double R;

Public Circle (double x) {

R=x;

}

Implement an abstract method in an interface to find the area of the circle

Public double area () {

Return PAI * R * r;

}

Public String toString () {

Return "Circle: r=" +r+ "\tarea=" +area ();

}

}

Program: Rectangle.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

Package Iekou;

public class Rectangle implements areainterface{

Double x, y;

Public Rectangle (double a,double b) {

X=a;

Y=b;

}

Public double area ()//To implement the abstract method in the interface, to find rectangular areas

{

return x * y;

}

Public String toString ()

{

Return "Rectangle: x=" +x+ "y=" +y+ "\ T"

Area=+area ();

}

}

One of the biggest differences between Java interfaces and Java abstract classes is that Java abstract classes can provide partial implementations of some methods, while Java interfaces are not, which is probably the only advantage of Java abstract classes, but this advantage is very useful. If you add a new concrete method to an abstract class, all of its subclasses get the new method all of a sudden, and the Java interface does not do this, and if a new method is added to a Java interface, all classes that implement the interface will fail to compile successfully. Because you have to let every class implement this method again, this is obviously a disadvantage of the Java interface.

The implementation of an abstract class can only be given by subclasses of this abstract class, that is, the implementation is in the hierarchy of inheritance defined by the abstract class, and because of the single inheritance of the Java language, the efficiency of the abstract class as a type definition tool is greatly compromised. At this point, the advantages of the Java interface come out that any class that implements a method specified by a Java interface can have the type of this interface, and a class can implement any number of Java interfaces, so there are many types of this class.

It is not difficult to see that the Java interface is an ideal tool for defining mixed types, and the hybrid class indicates that a class does not only have the behavior of one of the main types, but also has other minor behaviors.

In syntax, abstract classes and interfaces have the following differences:

1.abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. However, a class can implement multiple interface. The inheritance abstract class uses the extends keyword, implements the interface uses the Implements keyword, inherits writes in the front, implements the interface writes in the back. If you implement multiple interfaces, the middle is separated by commas. Cases:

public class Main extends JApplet

public class Main implements Runnable

public class Main extends JApplet implements ActionListener

public class Main extends JApplet implements ActionListener, Runnable

2. In abstract class, you can have your own data members, or you can have non-abstract member methods, and in interface, you can only have static data members that cannot be modified (that is, it must be static final, but Data members are not generally defined in interface, and all member methods are abstract.

The 3.abstract class and interface reflect a different design concept. In fact, abstract class represents the "is-a" relationship, interface represents the "like-a" relationship.

4. The class implementing the interface must implement all of these methods, inheriting from the subclass of the abstract class to implement all the abstract methods. Abstract classes can have non-abstract methods. There is no implementation method in the interface.

5. The variable defined in the interface is the public static final type by default, and must be given its initial value, so the implementation class cannot be redefined or changed.

6. Variables in abstract classes have friendly permissions by default, their values can be redefined in subclasses, or they can be re-assigned.

7. The methods in the interface are the public abstract type by default.

Java defines the interface format:

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.