Java interface and java Interface

Source: Internet
Author: User

Java interface and java Interface

Before introducing the interface, we should first understand its brother "inherit.

 

In object-oriented programming, inheritance avoids repeated definitions of common behaviors between multiple classes, saving both time and system resources. Inheritance seems to be of great help to us, but we should use less inheritance in actual programming. Why?

 

There are many reasons. I think the biggest reason is that inheritance emphasizes a "is a" relationship, that is, a "relationship.

 

For example, a sparrow is a bird and a carp is a fish. This will bring about a problem. Suppose there is something that can fly like a bird and swim in the water like a fish, then it must be a bird or a fish. Of course, the answer is no.

 

In this way, if multiple classes have the same behavior in the program, can they all inherit from one parent class? If you are not afraid of chaos, I think you can try it.

 

But you don't have to worry about it. Fortunately, the interface is coming. I can see a description of the interface on the Internet.

 

He said interfaces are a standard and there are many interfaces in life. For example, the tap pipe interface and power supply interface. In the past, before using the interface, the faucet manufacturer should provide both the faucet and the sewer pipe. They are one and cannot be changed. It also applies only to one of its products. With the interface, the manufacturer does not have to worry about how to design his sewer pipe. Instead, the manufacturer only produces the faucet according to the interface standard and directly connects it to the sewer pipe. This is the interface oriented, so that your sewer can use all the faucets that meet the sewer pipe standards, rather than the previous one. Obviously, changing the faucet is more convenient now.

 

An interface defines a behavior. Different classes can have the same behavior. In this way, interfaces and implementations are separated and suitable for collaborative development by teams. It is mainly used to implement loosely coupled systems for future upgrade and expansion.

 

In addition, inheritance is like the parent father of the class, and the interface is the father of the class. There is only one parent father, and many can be recognized by the father. In fact, it is also easy to understand. Inheritance emphasizes the "is a" relationship, of course, it can only be one. Interfaces define behaviors, and a class must have multiple behaviors.

 

At the specific syntax level, the interface keyword is used to define the behavior. Methods in the interface cannot be abstract and must be public.

For example, public interface extends mer {

Public abstract void swim ();

}

 

You must use the implements keyword to use the interface. At this time, there are two methods defined in the interface. One is the method defined in the operation interface, and the other is the abstract method.

For example, public abstract class Fish implements extends mer {

...

}

 

In addition, an interface can inherit other interfaces or more than two interfaces at the same time. It also uses the extends keyword to represent the behavior of inheriting the parent interface.

 

Most of the interfaces are used in relatively large projects. Therefore, when you are new to programming, you do not have the opportunity to access interfaces. As you learn more, I believe that you will have more feelings about the use of interfaces.

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.