Abstraction classes: Abstract class
Interface: interface
In a Java class, the class is an abstract class, as long as there is a method that is an abstract method. In other words, an abstract class can provide a specific implementation of a partial method, which is not allowed in the interface.
interface, you can only have the definition of a method, but not the method body.
With regard to abstract classes and interfaces, it is easy to see that they do not instantiate objects, so their only meaning is inheritance. If you say a class that inherits an abstract class, it is still an abstract class if the abstract method of the parent class is not fully implemented.
Abstract methods in abstract classes must be public or protect (the default is public), it is easy to understand that if the abstract method is private, then the subclass can not rewrite the method, so it is meaningless. In fact, a concrete method can be defined in an abstract class or a
It's a good thing, for a chestnut, when you want to add a new feature to a module, you just need to add a method to the abstract class to implement it, so all the implementation classes that inherit the abstract class have this method easily.
After talking about abstract classes, it feels like interfaces, so to speak, interfaces are more abstract and more restrictive than abstract classes.
The properties and methods in the interface are implicitly decorated as public static final and public abstract, although in general it is not recommended to place attributes in an interface. If you want to add a new abstract method to the interface, then all classes that implement the interface must implement
This abstract method is much more troublesome than the abstract class.
Having said so much, compare the differences between interfaces and abstract classes.
Java is a single-inheritance language (c + + is multi-inherited), so when an implementation class inherits an abstract class, its type is relatively single. And the interface is different, if a class implements all the abstract methods of an interface, then the class has this interface
Type, and a class can implement multiple interfaces, so the class has more types.
Another difference is that there can be static and static variables in an abstract class, and interfaces are not allowed.
So when exactly should they be used, the individual understands that the relationship between the abstract and the subclass is more biased toward "yes or no relationship" and prefers some intrinsic properties, such as the "Two hands and two feet" property. The relationship between the interface and the subclass is biased towards the "
Or no relationship ", say the man with a hat, the man without.
Found a very interesting example on the Internet.
Interfacewindow{ Public voidDraw (); Public voidPutcolor (); Public voidsetposition (); } Abstract classDesignedpageImplementswindow{ Public voidDraw () {} Public voidPutcolor () {}} Public classDesingedpageaextendsdesignedpage{ Public voidsetposition () {//Set the window position } }
There are three methods in this interface, but as an object inside the module, I just want to use his setposition () method, and I don't want to see it, that's what I'm going to do:
1. Create an abstract class designedpage, and then we do not want to see the method Draw () and Putcolor () plus go, the method body is set to empty (in fact, this writing indicates that already
These two methods are implemented in an abstract class).
2. Then ignore the method you want to implement SetPosition (), at which point it should be abstract for the class and not fully implement the interface.
3. Implement the SetPosition () method in the implementation class of the abstract class, so that the required functionality can be implemented conveniently.
As can be seen from here, the abstract class is "internal", and the interface is "external", to create a unified external caliber.
This can be written because abstract classes cannot be instantiated, so there is no requirement to implement all the methods. But the methods that are not written are implicitly existent.
When you are defining an implementation class to inherit an abstract class, you must implement it all.
Reference: http://fishswing.iteye.com/blog/1527166
Learn about the nature of Java inheritance, polymorphism, abstract classes, interfaces, and more (3)