Use of interfaces in Java

Source: Internet
Author: User

Abstract classes are templates that are abstracted from multiple classes, and if this abstraction is made more thorough, it is the interface (interface). What is an interface, simply speaking, an interface is a further abstraction of an abstract class, a further abstraction that defines only one specification without needing to be concerned with the details of the specific data state and method implementation, which specifies only a subset of the methods that must be provided. The following describes the specific use of the interface details;

1. The interface cannot contain constructors and initialization block definitions and can only contain member variables (static constants), methods (abstract instance methods, class methods, or Default methods), inner classes (internal interfaces, enumerations), three members.

2. All members of the interface should be defined as public permissions, this public can be omitted and the system will be automatically replenished. In the same vein, static constants defined in the interface automatically add static and final, so they can also be omitted. And because there are no constructors and initialization blocks, the member variables inside the interface can only be specified when the initial value is defined.

3. The method defined in the interface can only be an abstract method, a class method, or a default method, so the system automatically adds the public abstract adornment to the normal method, while the normal method cannot have a method body (abstract method). However, both the class method and the default method must have a method body.

4. The default method must have the defaults modifier, cannot be modified with static, and the same public is automatically incremented, the default method requires an instance of the class that the interface implements to invoke. Class methods must be static decorated, public automatically incremented, and class methods can be called directly using interfaces.

5. The interface supports multiple inheritance, that is, an interface can have multiple direct parent interfaces.

Here's a concrete example:

 PackageBiology; Public Interfaceanimal{intClassification = 7;    String eat ();    String move (); default voiddescription () {System.out.println ("I am a kind of biology"); }    StaticString Summary () {return"The Nature is wonderful"; }} PackageBiology; Public classDogImplementsanimal{ PublicString name;  PublicDog (String name) { This. Name =name; }     PublicString Eat () {returnName + "Eat meat and Grass"; }     PublicString Move () {returnName + "Move with dog ' s legs"; }} PackageBiology; Public classGoatImplementsanimal{ PublicString name;  PublicGoat (String name) { This. Name =name; }     PublicString Eat () {returnName + "Eat Grass"; }     PublicString Move () {returnName + "Move with goat ' s legs"; }} PackageBiology; Public classTigerImplementsanimal{ PublicString name;  PublicTiger (String name) { This. Name =name; }     PublicString Eat () {returnName + "Eat meat"; }     PublicString Move () {returnName + "Move with Tiger ' s legs"; }} PackageBiology; Public classtest{ Public Static voidMain (string[] args) {Dog animal1=NewDog ("Shepherd"); Tiger Animal2=NewTiger ("Bengal Tiger"); Goat Animal3=NewGoat ("Sheep"); System.out.println ("The Classification of biology is" +animal.classification); System.out.println (Animal1.name+ ":" + animal1.eat () + "." +Animal1.move ()); System.out.println (Animal2.name+ ":" + animal2.eat () + "." +Animal2.move ()); System.out.println (Animal3.name+ ":" + animal3.eat () + "." +Animal3.move ());        Animal1.description ();        Animal2.description ();        Animal3.description ();                    System.out.println (Animal.summary ()); }}

The results of the operation are as follows:

7 Shepherd:shepherd eat meat and grass. Shepherd move with dog's legsBengal tiger:bengal Tiger eat meat. Bengal Tiger move with Tiger's legssheep:sheep eat grass. Sheep move with goat's legsI am a kind of biologyi am a kind of biologyi am a kind of biologythe NAT Ure is wonderful

In this example, we define an interface animal, which defines a member variable classification (automatically add public static final modification, called by an interface), two abstract methods eat () and Move () ( Automatically add public abstract adornments, implemented by the interface implementation Class), a default method (called by an instance of an interface implementation Class), a class method (called directly by the interface).

That's about as much, and the details are more about reading and knocking at the code.

Use of interfaces in Java

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.