If a class implements an interface, it must implement all of the methods inside that interface
The returned type in Java is an interface, but an interface cannot be instantiated, but an interface can declare a reference to its implementation class, that is, the actual return value is an object of the implementation class for this interface. This is one of the main features of object-oriented.
Remember a word: The property looks to the left, the method looks to the right, for example:
Interface A = new Class1 ();
So if there is an identical attribute declaration in the interface and Class1, say in the interface: int num=5; Inside the class was likewise declared: int num=6;
So: a.num=5, because by default, the properties in the interface are decorated with public static final, so it cannot be modified by its implementation class, when the method is called, it is to the right,
such as: A.method (), then call is the interface of the instantiation class inside the method
The reference to the Java interface (here is that a) is actually the use of creating an object of the class that implements the interface (simply by using an object), to manipulate the properties and methods within the interface itself, and of course the operation of the method is called the method of implementing the implementation class. The benefit of declaring a reference to the object of its specific implementation class with an interface is that the reference can only invoke the method of the interface contained within the implementation class, but not the method that is unexpected in the interface except the method that belongs to the implementation class. We all know that an interface can be implemented by different implementation classes, of course, these different implementation classes in the implementation of interface methods, the definition of the specific functions are different, so a reference to an interface can be specific requirements to point to the specific implementation class. But it is not the same as the object of its implementation class, because the reference can only manipulate the properties and methods of the interface itself, but not the methods in the implementation class except the interface method,
Like what:
Public interface a{public
void Hello ();
}
public class B implements a{the public
void hello{
System.out.println ("hello!")
;
public void test{
System.out.println ("hello!");
}
public static void Main (string[] args) {
A a=new B ();
A.hello ();
A.test ();
}
where A.hello (): Can print out Hello, but a.test (); You can't print Hello, if you want to use a to invoke the test method in class B, you need to do a mandatory type conversion (b (a)). Test (); objects that are converted to type B can be aligned to call
The advantage of declaring a reference with an interface is that others do not know how you implement the method inside the interface, that is, do not know the interface of the implementation of Class B source code, can only use the reference of the interface to call B method to achieve the corresponding functions, like a car engine, you know how to use this engine, But you don't know how the engine is being implemented.