Abstract class
In object-oriented fields, everything is an object, and all objects are described by classes, but not all classes are intended to describe objects. If a class does not have enough information to describe a specific object and needs other concrete classes to support it, then such a class is called an abstract class.
Abstract class is modified by the abstract keyword, because he is abstract, that is, a class that exists on the consciousness level, so he cannot be instantiated, and the work of instantiation should be done by its subclasses.
1 Abstract class ClassName 2 {3 }
Abstract classes can define abstract methods in addition to the normal class, except that they cannot create objects, and the ordinary classes have all of them (including construction methods).
Abstract method: Modifier Abstract return value type function name (parameter list);
Modifier can only be protected or public, abstract methods can not write method body, the specific implementation method to the sub-class to write.
1 Abstract classClassName2 {3 intNum//a member variable that is protected decorated by default4 PublicString str;//Public member variables5 Public Static intNums = 10;//Static member variables6 Public Final Static intNUMSS = 10;//Constants7 PublicClassName () {8System.out.println ("This is a constructor. ");9 }Ten Abstract voidMethod ();//abstract method that is protected decorated by default One Public Abstract voidfunction ();//abstract methods for public permissions A}
abstract classes Under normal circumstances need to write abstract methods (do not write or error), but do not write, it is different from the ordinary class, and he can not be instantiated. Therefore, it needs to be inherited (without being inherited or error-free), but if it is not inherited, then the meaning of the abstract class exists.
Subclasses (if not abstract classes) must override all abstract methods in the abstract class. Therefore, abstract cannot be decorated with the same method as private, static, final, or native.
class extends ClassName// inherit the abstract function above { void method () {}// If the subclass is not an abstract class, You must override the parent class's abstract function public void function () {}// override the function's access permission to the parent class }
Abstract class Summary: Abstract classes are templates that are used to create inheritance-level lining classes.
Interface (interface)
Interfaces are a collection of method features that come from specific methods that conform to them and generally come from a number of methods that are constantly appearing in the system. An interface has only the characteristics of methods, and there is no implementation of methods, so these methods can have completely different behavior when implemented in different places.
Interface definition: Unlike the class definition, the definition interface no longer uses the class keyword, but instead uses the interface keyword.
1 Interface InterfaceName 2 {3 }
An interface is not a class, he has no constructors, and cannot be instantiated. There are only two types of members in the interface: constants and abstract methods.
Specification for constant notation: public final static [data type] constant name = value;
Because the public static/final modifier is always used, the system is automatically added even if the public static/final modifier is not added to the definition, so you can omit them.
The abstract method in the interface is the same as the abstract method in the abstract class.
However, because there are only abstract methods in the interface, and they always use the public abstract modifier, even if the definition does not add the public abstract modifier, the system is automatically added, so you can omit them.
1 InterfaceInterfaceName2 {3 intNUM1 = 1;4 Public intnum2 = 1;5 Public Static intNUM3 = 1;6 Public Final intNUM4 = 1;7 Public Final Static intNUM5 = 1;//in the interface, the above num1-num4 is a shorthand for the constant notation of NUM5.8 9 voidmethod1 ();Ten Public voidmethod2 (); One Abstract voidmethod3 (); A Public Abstract voidMETHOD4 ();//in the interface, METHOD1-METHOD3 is the shorthand for the Method4 method. -}
Interface Summary: An interface is a specification that the class that implements it needs to follow.
Classes, abstract classes, and interfaces
1. The method of an interface is an abstract method of public permission compared to an abstract class, so it is purely to be inherited and implemented, otherwise it will be meaningless.
2. Classes can only be single-inheritance (abstract classes are also classes), with multiple inheritance between interfaces.
3. A class can implement multiple interfaces (including one) at the same time as inheriting a class.
Interface extends Interface2,interface3// inherits two interfaces and can inherit multiple {}interface interface2{} Interface interface3{}// NULL interface class ClassName// empty class {} classextendsimplements interface2,interface3// can inherit only one class, Multiple interfaces can be implemented,{}
4. Abstract class implementation interface, can not rewrite the interface inside the abstract method, but the subclass of this abstract class (ordinary Class) must rewrite the abstract method of the interface.
5. The constants of the parent class (regardless of how many generations) can be called with a subclass, including the constants of the implemented interfaces. Abstract classes and interfaces can call their own constants directly with their own class name or interface name.
1 Public classBlog {2 Public Static voidMain (string[] args) {3System.out.println (THEINTERFACE.STR);//the interface calls its own constants4System.out.println (THEABSTRACT.STR);//constants for calling interfaces for abstract class names5System.out.println (CLASSNAME.STR);//constants that invoke the parent class interface6 }7 }8 Interfacetheinterface{9String str = "What happened!";//constant ShorthandTen voidMethod ();//shorthand for abstract methods One } A Abstract classTheabstractImplementstheinterface{//Abstract classes can not override the parent class or the abstract method of the implemented interface - Public voidMyMethod () { - } the } - classClassNameextendstheabstract{ - Public voidMethod () {}//the non-overridden abstract methods of the parent class (including the ' ancestor class ', the implements interface, or the ancestor interface) must be rewritten -}
Java abstract classes and interfaces