-
Interface (interface):
The interface is the same as class, in other words all methods in the interface are abstract methods. The abstract keyword can be used when declaring a method in an interface, or it may not, typically omitting the abstract keyword
-
Interfaces can be considered as special abstract classes (abstract classes can have specific methods or abstract methods, and interfaces can only have abstract methods, not specific methods)
-
class can implement interfaces. The implementation uses the keyword implement representation, which represents a class that implements an interface
-
A class implements an interface. Then the class must implement all the methods declared in the interface, and if the class is an abstract class, then there is no need to implement the methods in the interface
-
Java is a single-inheritance, that is, a class can only have a single parent class, a class may implement multiple interfaces, multiple interfaces separated by commas
Package com.yuanzijain02;interface myinterface{public void output ();} Interface myinterface2{public void Output2 ();} Class MyClass implements MyInterface, myinterface2{public void output () {System.out.println ("Output");} @Overridepublic void Output2 () {//TODO auto-generated method stub System.out.println ("Output2");}} public class Test3 {public static void main (string[] args) {MyClass MyClass = new MyClass (); Myclass.output (); MYCLASS.OUTPU T2 ();}}
6. A class can inherit a parent class at the same time, while implementing multiple interfaces
Package com.yuanzijain02;interface myinterface{public void output ();} Interface myinterface2{public void Output2 ();} Class Myparent{public void Output3 () {System.out.println ("Output3");}} Class MyClass extends Myparent implements MyInterface, myinterface2{public void output () {System.out.println ("Output");} @Overridepublic void Output2 () {//TODO auto-generated method stub System.out.println ("Output2");}} public class Test3 {public static void main (string[] args) {MyClass MyClass = new MyClass (); Myclass.output (); MYCLASS.OUTPU T2 (); Myclass.output3 ();}}
7. Polymorphism: A so-called polymorphic refers to a reference to a parent type that can point to an object of a subtype, or an application of an interface type can point to an instance of a class that implements the interface, as in the case of a forced type conversion between classes that implement an interface and the coercion of a class between a parent class and a subclass.
Package Com.yuanzijain03;public class Test4 {public static void main (string[] args) {//up type conversion/*aa BB = new BB ();//reference to interface type Point to Bb.output (); *///down type convert/*aa AA = new BB (); bb bb = (BB) aa;bb.output (); */}}interface aa{public void output ();} Class BB implements aa{public void output () {System.out.println ("BB");}}
Java Object-oriented interface