The idea of Java interface

Source: Internet
Author: User

the idea of an interface:

Example: Notebook PC, USB interface story.



1, the appearance of the interface to the function is to implement the extension.
2, the appearance of the interface defines the rule.
3, the appearance of the interface reduces the coupling (decoupling).

The appearance of the interface, the completion of the understanding of decoupling, indicating that there are two parties, one party in the use of this rule, the other party in the implementation of this rule. For example, laptops use this rule, while peripherals implement this rule.


The difference between an interface and an abstract class

Describe things.
Dog. Categorized by function. Guide dog, drug dog ...

Dog:
Roar ();
Eat ();

<span style= "FONT-SIZE:18PX;" >abstract class Dog {public abstract void roar ();p ublic abstract void Meal ();} Class drug dog extends dog {public void roar () {}public void meal () {}public void drug () {}}//for narcotics, there may also be a drug-anti-narcotics pig, with the anti-narcotics function, should be the anti-drug function extraction. A description of the drug. Abstract class anti-narcotics {public abstract void narcotics ();} </span>

Drug dogs need both the function of dogs and the function of drugs.
Cannot directly inherit more.
Is it possible to implement more? OK.
Dogs are interfaces, and narcotics are also interfaces. Anti-drug dogs can be realized.
The class is responsible for describing the basic functions of things. The interface is responsible for describing the extended functionality of things.
A drug dog is one of the dogs. is a relationship,
Defines a dog as a class. and anti-drug is an extended function of dogs. At this point the drug definition interface.

The description then becomes this:

<span style= "FONT-SIZE:18PX;" >interface Anti-narcotics able{public Abstract void Narcotics ();} Class drug dog extends dog implements drug able{public  void Roar () {code:} Public  void Meal () {}public  void drug () {}}</span>

The drug dog does not have its own function, all of his functions come from the parent class and interface. extraction is to extract a common function, in the drug dog Although the code is not changed, but there is no definition of class, it only needs to perfect the content on it.

Section:
1, abstract class is the basic function of describing things, can define non-abstract methods.
Interfaces are defined only as abstract methods, which are responsible for the extension of functions.
2, the class is the inheritance relationship between the class is a relationship.
A relationship between a class and an interface is an implementation relationship like a.


The idea of Java 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.