Implementation of the Java Section 38th interface

Source: Internet
Author: User

2016-06-29

1 implementation of the interface
Class name implements interface 1, Interface 2, interface 3{
Method 1 () {
}
Method 2 () {
}
...
}

Note implementation of the interface implementation:
1. Provide specific implementations for all the methods in the interface.
2. All rules of rewriting must be complied with.
Rewrite rule:
1. The overriding method of a subclass cannot throw a larger exception.
2, the subclass of the overriding method can not have a smaller access scope.
Parent class: public void Test ();
Subclass: public void Test ();
protected void test (); wrong
3. Keep the same return type.

Multiple inheritance means inheriting more than one interface
1. These interfaces need to be separated by commas.
2, if these interfaces all have the same method, the same variable,
Then the same variable can be accessed by the name of the interface name.
The same method will be used by one of the interfaces.

First, define the interface

 Packagecom.java1995;/*** Define the interface: Students *@authorAdministrator **/ Public InterfaceStudent {intbanji=1201;//class NumberString name= "Xiao Ming"; voidLearn ();//Learning Methods    voidEat ();//How to eat    voidSleep ();//Sleeping Methods    }
 package   com.java1995;  /**   * define interfaces: People *   @author   Administrator *  */ public  interface          people {String name  =" Lao Wang "< span> void  learn (); //     learning method  void  eat (); //     dinner method  void  sleep (); //  sleeping method    

Second, realize the interface

 Packagecom.java1995;/*** Implement Interface * Multiple inheritance: Inherit multiple interfaces *@authorAdministrator **/ Public classStudent1Implementsstudent,people {@Override Public voidLearn () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("The students are learning!") "); } @Override Public voideat () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("The students are eating!") "); } @Override Public voidsleep () {//TODO auto-generated Method StubSYSTEM.OUT.PRINTLN ("The students are eating!") "); }}

Third, call the interface

 Packagecom.java1995;/*** Call interface *@authorAdministrator **/ Public classteststudent { Public Static voidMain (string[] args) {Student1 stu=NewStudent1 ();        Stu.learn ();        Stu.eat ();                Stu.sleep (); //Public static Final//Stu.banji=1; Verify that the final typeSystem.out.println (Student1.banji); System.out.println (Student.name+":"+people.name); }    }

Resources

[1] Java Easy Start Classic tutorial "full version"

Implementation of the Java Section 38th 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.