Discussion on interface application in Java programming

Source: Internet
Author: User
Tags abstract final getmessage multiple inheritance in c

The Java language provides an interface (interface) mechanism. This interface mechanism makes Java object-oriented programming more flexible. We can use interfaces to define the representation of a class, but an interface cannot contain any implementations. In the book Thinking in Java, the author describes the interface as follows: "The interface (interface) is a step closer to the concept of abstraction (abstract)." You can think of an interface as a pure abstract class. "I think the author's interpretation of the interface is more accurate," he said.

Understanding and using a good interface mechanism will help us better master Java, an object-oriented programming language. Now let's discuss the usage rules for interfaces and the related applications.

The definition and realization of interface

Defining interfaces is similar to defining classes, except that the class keyword is replaced with interface. When you define a method, you need only the method name, which returns the type and argument list, and cannot have a method body. You can define fields in an interface that are implied as static and final, so you should set the values of these fields as needed. For example:

public interface Flyable {
void fly();
}
public interface Talkable {
void talk();
}
public interface Message {
int MAX_SIZE = 4096;
String getMessage();
}

In several of the interfaces defined above, flyable and talkable only define one method, and the message has a field max_size in addition to the method. It can be seen that these interfaces define only the representations of the class, not any implementations, and therefore cannot be used directly. To use these interfaces, you need to have classes to implement them. When implementing an interface, you should first declare the interface to be implemented with the Implements keyword after the class name, and if you want to implement multiple interfaces, you should separate them with commas and implement the methods defined in those interfaces. As in the following example:

public class Parrot implements Flyable, Talkable {
public void fly() {
System.out.println("Flying like a parrot…");
}
public void talk() {
System.out.println("Hello! I am a parrot!");
}
}
public class TextMessage implements Message {
String message;
public void setMessage(String msg) {
message = msg;
if (message.length() > MAX_SIZE)
message = message.substring(0, MAX_SIZE);
}
public String getMessage() {
return message;
}
}

In the Parrot (Parrot) example, we use interface flyable to express the ability to fly, talkable to speak, but they do not contain concrete implementations. And Parrot has both of these capabilities, so we have both flyable and talkable interfaces for the Parrot class. We can also define a swallow (Swallow) class, but the swallow only has the ability to fly, so we just need to implement flyable for swallow. Because of their different methods of flight, they have their own specific implementation of the flight.

In addition, because a class can implement multiple interfaces at the same time, the object-oriented nature of Java becomes very flexible. With this feature, we can implement features such as multiple inheritance in C + + languages, and even more flexible features. Now let's discuss the application of interfaces in practice.

Two, use the interface to define some global variables

Because the fields within the interface are static and final, we can easily use this to create some constants. For example:

public interface Constants {
String ROOT = "/root";
int MAX_COUNT = 200;
int MIN_COUNT = 100;
}

The constants can be referenced directly in the form of constants.root when used. We can also use this method to create constants with an indeterminate initial value.

public interface RandomColor {
int red = Math.random() * 255;
int green = Math.random() * 255;
int blue = Math.random() * 255;
}

The values of red, green, and Blue are established on first access and remain unchanged.

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.