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