- The term "implementation" in the text refers specifically to the inheritance of interfaces.
- When a class implements multiple interfaces, the default method with the same name cannot occur.
- A class must implement both interfaces and abstract classes, inheriting them first.
- An abstract class can inherit multiple interfaces (implements), an interface cannot inherit an abstract class, and an interface can inherit multiple interfaces (extends).
- The default method in the interface, the abstract method is omitted, the data member must be assigned the initial value, final can be omitted.
- The role of interfaces is to establish standards, a code that all parties need to follow.
- In order to simplify the client (not listing all the objects to choose from), the interface is taken as an example, and the Factory mode is preferred.
public class Test1 {
public static void Main (string[] args)
{Fruit a = factory1.getinstance ("Apple");
A.eat ();
}}
Interface Fruit
{
public void eat ();
}
Class Apple implements Fruit
{
public void Eat ()
{
System.out.println ("Eat apples");
}
}
Class Orange implements Fruit
{
public void Eat ()
{
System.out.println ("Eat oranges");
}
}
Class Factory1//Get instance object of fruit class
{
public static Fruit getinstance (String classname)
{
if ("Apple". Equals (classname))
return new Apple ();
if ("Orange". Equals (classname))
return new Orange ();
return null;
}
}
At this time the program, the client (main method) is not coupled with the specific subclass, if there are more Friut sub-class interface only need to modify the factory class, all interface objects are obtained through the factory class, in the development
You should use factory design mode whenever you encounter an operation that obtains an instance of an interface object.
Java Interface----Inheritance (Implementation) method