Java Multi-State Learning 4

Source: Internet
Author: User

OK, well, this one sectionto to talk about the interface, hey, or old, the reader is best to use the above-mentioned abstract class to compare Oh O (∩_∩) o haha ~

What is an interface?
Interface is to give some methods without content, encapsulated together, to a class to use, in accordance with the specific circumstances of these methods written.

接口的建立语法:interface 接口名{方法;}语法:class 类名 implements 接口{        方法;        变量;    }

Summary: The interface is a more abstract abstract class, the method in the abstract class can have the method body, all the methods in the interface have no method body. The interface embodies the design idea of multi-state and high-cohesion low-coupling in programming.

Interface – Considerations
1. Interfaces cannot be instantiated
2, all methods in the interface can not have the subject. Error syntax example: void aaa () {}← (note cannot have curly braces)
Interfaces can be thought of as abstract classes that are more abstract.
3, a class can implement a plurality of interfaces
4, the interface can have variables [but variables cannot be modified with private and protected]
A, the variables in the interface are essentially static and final type, whether you add static modifiers or not
b, in Java development, we often use common variables, defined in the interface, as a global variable used
Access form: interface name. variable Name
5, an interface cannot inherit other classes, but can inherit other interfaces

Now, let's take a look at the application of the interface.

Package Interfacekak;//mouth Interface//interface Mouth extends base{//error, interfaces can only inherit from interfaces and cannot inherit from class interface Mouth extends cell{    intA=1;//Add or static is still, can not be modified with private and protected    //Declaration of two methods     Public voidEat ();//Mouth to eat     Public voidDrink ();//Mouth drink    /*public void Sing ()//error, {System.out.println ("Everybody Sing together."); };//Mouth Sing * *}//wrote a dog class, and implemented the mouth interface//An important principle: When a class implements an interface, it requires that the class implement all the methods of this interface class Dog implements Mouth{     Public voidEat () {System.out.println ("The dog wants something to eat."); } Public voidDrink () {System.out.println ("The dog wants something to drink."); }}//interface inherits other interfaces class Base{} interface cell{}//wrote a cat class, and implemented the mouth interface class Cat implements Mouth{     Public voidEat () {System.out.println ("The cat wants to eat something."); } Public voidDrink () {System.out.println ("The cat wants to drink water."); }}//Animal type class Animal{    //start using the mouth interface     Public voidActivity (Mouth Mouth) {mouth.eat ();    Mouth.drink (); }} Public  class interfacekak {     Public Static voidMain (string[] args) {//TODO auto-generated method stub        //mouth.a++; A is static and cannot be changedSystem.out.println (MOUTH.A);//Create AnimalAnimal animal=NewAnimal ();//Create dogDog dog1=NewDog ();//Create catCat cat1=NewCat (); System.out.println (DOG.A);//Can class can access the variables of the interfaceAnimal.activity (DOG1);    Animal.activity (CAT1); }}

Results

1, the implementation of the interface can be seen as a complement to inheritance. (Inheritance is hierarchical and less flexible.) Modifying a class breaks the balance of inheritance, and the interface has no such trouble because it only works on classes that implement the interface)
2, the implementation of the interface can not break the inheritance relationship under the premise of a class function extension, very flexible.

PS: What is the comparison between a good and an abstract class?

Big Brother, can you pay attention to my two articles, abstract class and interface of the note, compared to a good, master with the door, practice on individual ah ....

Not enough, introduce someone else's website of the Great God, we go to visit it, small temple small Ah, fled%>_<%
"The difference between abstract class and interface"
http://blog.csdn.net/ttgjz/article/details/2960451

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java Multi-State Learning 4

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.