Why interfaces can not have constructors

By way of example when it comes to how to distinguish between abstract classes and interfaces, where we Starting from Java syntax, so that we better understand the inherent difference between the two. Their grammatical differences: 1) the interface can not have a configuration method, there may be an abstract class. 2) not have an interface method thereof, there may be an abstract class. 3) the interface can have a static method, there may be an abstract class. 4) all variables in the interface must be public static final, but not required in an abstract class.
Suddenly there is this article, because the students doubts: abstract class constructor actually there, they can not be directly used for new. My explanation is usually present in the constructor for an abstract class, not impressed, because IDE generates a default constructor with no arguments you can also write constructor explicitly, this construction method is used subclass called, because any subclass must call the father of all constructors Object from the beginning, to complete initialization. So I extended look, they asked, interfaces have constructors do? Their understanding, abstraction can have, why not have an interface? ! So the interface in writing constructor, the compiler Tip: Interfaces can not have constructors. How can this solution?
From the syntax point of view, there must be an abstract class constructor, and the interface is strictly prohibited construction method, which also illustrates their different properties. An abstract class is a class, other classes are inherited by the keyword extends to, and expand, there is a very strong relationship is-a, this relationship generally in line with Richter substitution principle . And the interface, by another class is a method used to implement the keyword implements the interface definition. If no difference, why the whole out of two different keywords. Interface specification defines only function and behavior, if a class implements an interface, then the class must comply with the methods of the interface conventions, but not the relationship is-a. The "on the wall pupils code of conduct " imagine an interface, so that students must comply with this convention, but not the students' code of conduct. "
Constructor is used to perform some pre-processing before the target object initialization, it provides access to a specific example of the thing. Interface just declare it, not necessarily what they want to initialize, even if you want to initialize can also go to those classes which implements the interface to initialize. Only used to describe the operation of the interface, the expression of the specification may be a new Computer, but we are not a new IDE, SATA, PCI, PS- 2. Thus, the interface method to construct what purpose? Interface is a specification is called, the main concern is inside the method, and the method does not require initialization, a class can implement multiple interfaces, if multiple interfaces has its own constructor, the constructor bad decision the calling sequence, is configured to belong to the class of their own, it can not be inherited, as pure virtual, the interface does not require the constructor. The abstract class is the ancestor of concrete classes, even the Stone Age, but also always want to do some initialization work, although abstract class can not be instantiated directly, but the examples are subclasses of the time, it will initialize the parent class, regardless of the parent class is is not an abstract class, will call the parent class constructor to initialize a class, initialize the parent class, say there is no initialization interface.
Let's use an example to illustrate the difference between the two cars of, Car, Track, Van Vehicle is a subclass of the abstract class, then you can say Jiao car is a car, the car is a tractor, truck is the car. And as their common parent class, we will do some initial work, such as petrol, oil, coolant. Also a common attribute of all the car wheels, steering wheel, accelerator and the like. Brake is a brake action interface, this specification requires the car to achieve it, must have the ability to speed reduced to zero, but not the same way to achieve or brake drum or disc brakes. Or is not the same effect, and some brakes a little spiritual, some brake almost.
Where are abstract classes and interfaces can be considered, first by promoting the use of the interface, there is no relationship is-a in the syntax to use more flexible, and more can be achieved, after all, another is a rare resource. The abstract class when there is the realization of our common method, or when there is a lot of property operations, is the first choice when considering the use of an abstract class, in the foreseeable future, it is time to reflect inheritance is-a very strong relationship.
Interfaces can not have a method body is forced to the interface interface definition can not increase the number of detailed implementation, specified must be pure virtual interface for the architecture are concerned, it needs to do is to define an understandable interface name, parameter list as well as the return type . The abstract class can have its own implementation method, this method can be used to be performed. Sometimes, we will think of the abstract class methods can not use super show called because super is to refer to the parent object, while the abstract class objects can not have MS. But in fact, the actual object of any child classes, objects can be understood as a parent class.
All interface methods are abstract, and abstract method is not static, there can not override static methods can not achieve polymorphism, so this makes sense to define interfaces, Objective defined in the interface is very clear: to achieve class to implement, if you (nothing to do with specific examples) in the static interface method declaration, but the interface methods and requirements must be implemented to achieve the class (there may be multiple implementation class), is it not self contradict! If you insist on adding up the static compiler-friendly tips: Illegal modifier for the interface method FF.f (); only public & abstract are permitted. After all, you can not add any modifiers public and any addition to the abstract, such as:. Private, protected, final, static
data members no concept of polymorphism, Can be accessed only say, if a member is public data can be accessed and modified at any place, then the interface definition data members if it is private, why the definition, because it is no way, that there is no way for this data members can operate. If it is public, anyone can modify, then multiple implement multiple interfaces, this data is not changed too confusing? So the interface is generally not allowed to have data members, but if there is, the default is the clear public final static, strictly guaranteed for all implementations are concerned only copy of the original data.
So in architecture designWhen, how abstract classes and interfaces to solve the problem is a very complex issue, an abstract class is more focused on group together subclass of the same parent class, if the properties, methods; the interface is more focused on the definition of any class has no the same semantic method, which is a specification, once defined, is not easy to change, it changes in the project, often moving hair and pull the whole body, even if there is no good place to consider is also used to make up the form of the new interface . In fact, the abstract class is the "is-a" relationship, interface represents the "like-a" relationship. The top of the interface, and then implement the interface is an abstract class, and finally to a specific class implementation.

Guess you like

Origin www.cnblogs.com/mike-JP/p/10962555.html