Interfaces, abstract classes and implementation classes
1. Interface is the most abstract layer, without any implementation; abstract layer can implement part of the interface method, but also can customize the method; implementation class must implement all methods.
2. Interfaces can be implemented by classes (abstract classes and concrete classes). The difference is that abstract classes implement interfaces. They can implement or not implement interfaces. Specific classes must implement all methods of interfaces.
3. Interfaces can be inherited by interfaces, but not by classes.
4. Classes can inherit from each other and can only inherit from one another.
5. abstract means abstract. In java, it is stipulated that only classes or methods can be modified, so attributes cannot be modified. Contents modified by abstract are not yet implemented, such as classes and methods. The reason attributes cannot be abstractly modified is that attributes do not exist in the "not yet implemented" state. For example, you might think of int age; or String name; unfortunately, when declaring a variable, int defaults to an initial value of 0 for age, and String defaults to an initial value of ". Therefore, attributes can not reach the "unrealized state" and can not be abstractly modified.
6. The abstract class and ordinary class are: abstract class can not be instantiated, on the one hand, abstract method is similar to interface method; on the other hand, non-abstract method is the same as ordinary class method, so it combines two special types of interface and common class.
7. Because abstract classes cannot be instantiated, they are usually extended and instantiated as parent classes by subclasses. Therefore, all methods or variables above the level of protected of abstract classes can be invoked in subclasses.
- Create two new interfaces Context and Invocation
Context
package com.whc.abs; /** * Interfaces are the most abstract layer, defining only the interface functions and functions that need to be implemented. * * @author Administrator * @date 2019/4/11 */ public interface Context { // The member variable defined in the interface defaults to static String name = "admin"; int age = 18; Object getName(); Object getAge(); void set(); }
Another interface Invocation
package com.whc.abs; /** * @author Administrator * @date 2019/4/11 */ public interface Invocation { Object getSrc(); Object getTar(); Object getCreateTime(); Object getCreator(); }
Create a new abstract class AbstractContext
package com.whc.abs; import java.util.Date; /** * Abstract class implements interfaces, which can be implemented either abstractly or concretely * * @author Administrator * @date 2019/4/11 */ public abstract class AbstractContext implements Context, Invocation { /** * Override the attribute name of the interface class */ public String name = "admin1"; /** * Abstract classes can write new definitions of methods * * @return */ protected String getAccount(){ return "100"; } /** * A method that implements interfaces in abstract classes * * @return */ @Override public Object getName(){ return "admin"; } /** * Similar interface functions can be defined in abstract classes, but abstract fields must be added. * * @return */ abstract Object getEmail(); /** * The Method of Implementing Interface with Abstract Classes * * @return */ @Override public Object getCreateTime() { return new Date(); } /** * The Method of Implementing Interface with Abstract Classes * * @return */ @Override public Object getCreator() { return "administrator"; } }
The abstract class implementation class AbstractContextImpl
package com.whc.abs; /** * @author Administrator * @date 2019/4/11 */ public class AbstractContextImpl extends AbstractContext { /** * Existing methods of abstract classes can be overridden in implementation classes * * @return */ @Override public Object getName() { return "guest"; } /** * The method in the interface needs to be implemented * * @return */ @Override public Object getAge() { return "25"; } /** * The method in the interface needs to be implemented */ @Override public void set() { } /** * The method in the interface needs to be implemented * * @return */ @Override Object getEmail() { return null; } /** * The method in the interface needs to be implemented * * @return */ @Override public Object getSrc() { return null; } /** * The method in the interface needs to be implemented * * @return */ @Override public Object getTar() { return null; } public static void main(String[] args) { AbstractContext context = new AbstractContextImpl(); System.out.println(context.name);//Accessed is the name member of AbstractContext, which covers the static member name of the interface class. System.out.println(context.age);//What is accessed is the interface definition static variable age System.out.println(context.getAccount());//What we access is the way to customize the implementation of abstract classes System.out.println(context.getName());//The method of this implementation class is accessed because the implementation class overrides the method of the abstract class. System.out.println(context.getAge());//The method of this class is visited, and the method of Context interface is realized. } }
Output results:
admin1 18 100 guest 25
github address: