Learning abstract classes and interfaces compare experiences

Abstract class
1.abstract class moderation is interposed between the general class and interface
2. If a class contains one or more abstract methods, then this class must be declared as abstract, otherwise the compiler will error
3. You can not create objects of an abstract class, even if this is not an abstract class abstract method
4. inherited from a case of abstract class, if you want to create a new class of object, then the new subclass must implement all abstract methods of the abstract class in a subclass; otherwise, the subclass must also be made the abstract keyword modified abstract class
5. an abstract class may not contain any abstract methods; you have but not necessary to have a class abstract method, and is useful when you want to prevent any instances of that class

Interface
1.interface is more pure than the abstract concept of abstraction in the interface a little bit to realize there is no; the interface allows the creator to decide the method name, parameter list, return type, but no method body 
The method defined in 2.interface not need to add access modifier, because whatever you did to the public keyword, define the interface methods are in default must also be public; whether you did to abstract keyword in interface the method definitions are by default must also be abstract, and this is very useful!
Modifier field values ​​3.interface in default must also be public static final, so for example the definition of String name = "zhang" in the interface; even without modifiers public static final modification then the name is also a public static final;
4. If the derived class inherits from a non-interface type, you can only inherit a class; the rest are other interfaces can be inherited, implements key can be followed by any number of interface, the interface to use a comma in the back; and you can put the derived class upcast any interface
5. If a class with both extends keyword to inherit a class and implement the interface with the implements keyword, then the first write and then write implements extends inherited class that implements the interface or the compiler error;

6. In the actual development, we have to preferred interfaces rather than abstract class, rather than the preferred class interface, interface or need to have good reason to use when abstract, otherwise it is only when the code refactoring is most useful
7. It would be added by inheriting new methods in the new interface, and is also used in conjunction with several interfaces generate a new interface;

Guess you like

Origin blog.csdn.net/baidu2030/article/details/42098863