From: http://blog.chinaunix.net/uid-25885064-id-3361989.html
Java allows an abstract method. It is just a name without specific implementation. For example, public abstract void ABC (); uses the abstract keyword and ends. Classes that contain one or more abstract methods are called abstract classes. Abstract classes must also declare abstract keywords. The use of abstract classes has some restrictions, so you cannot create instances of abstract classes. If a subclass implements an abstract method, you can create an instance object for this subclass. If the subclass is not implemented, this subclass is also an abstract class and cannot be used to create instances.
What is an interface? Interfaces are more abstract classes than abstract classes. For example, the methods in the public interface name {} interface are all abstract, and all the variables in the interface are final constants. The class that implements the interface must implement all the abstract methods. Abstract classes can also have specific methods. Therefore, the interface is the most abstract, followed by the abstract class, and the specific class itself is the abstraction of the real world. Software Development itself abstracts the real world into a computer world.
Abstract classes and interfaces are more abstract than specific classes, so they are always inherited and implemented during use. But the classes that inherit them are not just one. There are many classes that implement their abstract methods. One method has multiple implementation methods. polymorphism in OOP is used here. This makes the design very clear. Because the base class is an abstract class or interface, there are several inherited classes under it. You only need to operate on the interface or abstract class, and you do not need to worry about how many implementations are there. It makes sense to develop projects jointly by multiple people.
Keyword implement instead of extends is used for interface implementation. If extends is used, this interface is inherited. That subclass is also an interface, which is the original subinterface.
- // Declare an Interface
- Interface say {
- Public void saymessage ();
- }
- // Two implementation classes
- Class sayhello implements say {
- Public void saymessage (){
- System. Out. println ("hello ");
- }
- }
- Class sayhi implements say {
- Public void saymessage (){
- System. Out. println ("hi ");
- }
- }
- Public class testsay {
- Public static void main (string [] ARGs ){
- // You can also use the say interface type instance to output two results.
- Say = new sayhello ();
- Say. saymessage ();
- Say say1 = new sayhi ();
- Say1.saymessage ();
- }
- }
The interface also plays an important role. Java only has single inheritance, that is, it can only inherit from a parent class. The benefit of single inheritance is that, once too many classes are inherited, The subclass changes. What if I want to inherit the features of multiple parent classes? Let's just use the interface. This class can inherit a class first, and then implement other interfaces. The interfaces are all abstract methods, which will not cause a systemic effect. Changing the multi-inheritance feature is also an improvement for the c ++ language.
There is a saying in the industry that Java is not so much object-oriented programming as interface-oriented programming. The emphasis is on the abstract description of interfaces. It is also an improvement for C ++, and there is no interface in C ++. Therefore, the Java language is suitable for large projects that involve multi-person teams. You can simply look at an interface. How can we implement it later.