Abstract class:
1. If one or more abstract methods exist in a class, the class must be declared as an abstract class and cannot be instantiated.
2. If you inherit from an abstract class and want to create the object of the new class, you must provide method definitions for all abstract methods in the base class.
If this is not done, the inverted class is also an abstract class, And the compiler will force the abstract keyword to be added.
Public abstract class abstractclass // there must be at least one abstract method {public int t; // common data member public abstract void Method1 (); // abstract method, the subclass of an abstract class must implement the abstract method public abstract void method2 (); Public void method3 (); // non-abstract method public int method4 (); public int method4 (){...... // The default behavior that can be granted to non-abstract method methods in the abstract class, that is, the specific implementation of the method} public void method3 (){...... // The default behavior that can be granted to non-abstract methods in the abstract class, that is, the specific implementation of the method }}
Interface:
1. Once an interface is implemented, it becomes a common class and can be expanded at will.
2. No methods in the interface are declared as public. They should be public by default.
All attributes declared in the interface are final and static by default.
Public interface {static final int I; // The interface cannot contain common data members. It can only have static data members that cannot be modified. Static indicates global, and final indicates unchangeable, static final modification is not required and is implicitly declared as static and final public void Method1 (); // methods in the interface must be abstract methods, so public void method2 () is not required (); // The interface cannot assign the default behavior of the method, that is, there cannot be a specific implementation of the method}
Summary:
In short, an abstract class is a class with incomplete functions. An interface is only a set of abstract method statements and static data that cannot be modified. Neither of them can be instantiated.
In a sense, an interface is a special form of abstract class. in Java, an abstract class represents an inheritance relationship. A class can only inherit one abstract class, A class can implement multiple interfaces. In many cases, the interface can indeed replace the abstract class, if you do not need to deliberately express the inheritance of attributes.