1, interface can inherit interface? is an abstract class achievable (implements) interface? Can abstract classes inherit concrete classes (concrete Class)? Can I have a static main method in an abstract class?
Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, and whether abstract classes can inherit concrete classes. There can be static main methods in an abstract class.
Note: As long as you understand the nature and role of interfaces and abstract classes, these questions are good answers, and you think, if you are the Java language Designer, will you provide such support, if not provided, what is the reason? If you have no reason not to provide, then the answer is yes. Just remember that the only difference between an abstract class and a normal class is that you cannot create an instance object and allow an abstract method.
2, when writing the Clone () method, usually have a line of code, what is it?
Clone has a default behavior, Super.clone (), because the members of the parent class are first copied into place, and then the members are copied.
3. What are the aspects of object-oriented features
Computer software system is a real-life business in the computer mapping, and real-life business is actually a process of object collaboration. Object-oriented programming is to organize and write program code in the same way as the real business, so that the computer system can recognize and understand the program code organized and written by object, so that the business objects in real life can be mapped to the computer system. The object-oriented programming language has 4 main features: encapsulation, inheritance, abstraction, polymorphism, etc.
Encapsulation: Encapsulation is to ensure that the software components have a good modularity of the foundation, the goal of the package is to achieve "high cohesion, low coupling" software components, to prevent program interdependence and the impact of the changes. In object-oriented programming languages, objects are the most basic unit of encapsulation, and object-oriented encapsulation is clearer and more powerful than traditional language encapsulation. Object-oriented encapsulation is to encapsulate the code describing the properties and behavior of an object in a "module", which is a class in which the attribute is defined by a variable, and the behavior is defined by the method, which directly accesses the attributes in the same object. In general, just remember to let the variable and access to the variable in the method put together, the member variables in a class are all defined as private, only the class of their own methods can access to these member variables, which basically implement the object encapsulation, it is easy to find the method to assign to this class, is basically object-oriented programming. Grasp a principle: Put the method of operation of the same thing and the related method in the same class, put the method and its operation data in the same class. For example, a person wants to draw a circle on the blackboard, which involves three objects: the person, the blackboard, the circle, and the circle of the method to be assigned to which object? Because the circle needs to be used to the center and RADIUS, the center and radius of the circle is obviously a property, if they are defined in the class as a private member variable, then the circle method must be assigned to the circle, it can access to the center and radius of the two properties, the person will just call round the circle method, to send a message to the circle Circle This method should not be assigned to the object of human, this is object-oriented encapsulation, the object is encapsulated into a highly autonomous and relatively closed individuals, the object state (property) by the object's own behavior (method) to read and change. A more understandable example is when the driver stops the train, the brakes are assigned to the driver, or the train is assigned to the train, and obviously should be assigned to trains, because the driver himself is unlikely to have that much strength to stop a train, only the train can do it, Trains need to call the internal clutch and brake pads and other devices to work together to complete the braking action, the driver's braking process just sent a message to the train, informing the train to perform the brake action.
Abstraction: Abstraction is the identification of similarities and commonalities of things, and then the attribution of these things to a class that considers only the similarities and commonalities of these things, and ignores those aspects that are irrelevant to the current subject and goal, focusing on aspects related to the current goal. For example, when you see an Ant and an elephant, you can imagine the similarities between them, which is abstraction. Abstraction includes two aspects of behavioral abstraction and State abstraction. For example, define a person class, as follows: Class Person {String name; int age; People are inherently complex, and there are many aspects, but because the current system only needs to know the name and age of the person, the class defined above contains only the names and ages of the two attributes, which is a kind of image, and using abstractions avoids the consideration of some details that are not related to the goal. My understanding of abstraction is not to use the microscope to see all aspects of a thing, so that involves too much content, but to be good at dividing the boundaries of the problem, what the current system needs, just consider what.
Inheritance: When defining and implementing a class, it can be done on the basis of an already existing class, the content defined by the already existing class as its own content, and the addition of a number of new content, or modify the original method to make it more suitable for special needs, which is inheritance. Inheritance is the mechanism by which subclasses automatically share the parent data and methods, which is a relationship between classes, which improves the reusability and extensibility of the software.
Polymorphic: polymorphism refers to the specific type of reference variable defined in the program and the method call issued through the reference variable is not determined during programming, but is determined during the program's run, that is, a reference to which the variable bottom will point to which class of the instance object, the reference variable emitted by the method call is exactly what class implemented in the method, Must be determined by the time the program is running. Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism. Polymorphism enhances the flexibility and extensibility of the software. For example, the Userdao in the following code is an interface that defines a reference variable Userdao the instance object pointed to by Daofactory.getdao () is returned at execution time, sometimes pointing to the Userjdbcdao implementation, Sometimes pointing to the implementation of Userhibernatedao, so that, without modifying the source code, you can change the Userdao point to the specific class implementation, resulting in Userdao.insertuser () method invocation of the specific code also changed, That is, sometimes called the Insertuser method of Userjdbcdao, sometimes called Userhibernatedao Insertuser method: Userdao Userdao = Daofactory.getdao (); Userdao.insertuser (user);
4. What is the mechanism for polymorphism in Java?
A reference variable that is defined by a parent class or interface can point to a subclass or an instance object of a specific implementation class, and the method called by the program is dynamically bound at run time, that is, the method that refers to the specific instance object that the variable points to, that is, the method of the object that is running in memory, rather than the method defined in the
5. What is the difference between abstract class and interface?
Class that contains the abstract modifier is an abstract class, and the abstract class cannot create an instance object. A class containing an abstract method must be defined as an abstract Class,abstract class in which the method does not have to be abstracted. Abstract class definitions must be implemented in a specific (concrete) subclass, so there can be no abstract constructor or abstract static method. If the subclass does not implement all the abstract methods in the abstract parent class, then the subclass must also be defined as an abstract type. An interface (interface) can be described as a special case of an abstract class, and all methods in an interface must be abstract. The method definition in the interface defaults to the public abstract type, and the member variable type in the interface defaults to public static final.
Here's a comparison of the syntax differences between the two:
1. Abstract classes can have construction methods, and interfaces cannot have constructors.
2. There can be ordinary member variables in the abstract class, there are no ordinary member variables in the interface
3. Abstract classes can contain non-abstract ordinary methods, all the methods in the interface must be abstract, and cannot have non-abstract ordinary methods.
4. Abstract methods in an abstract class can have access types public,protected and (the default type, although Eclipse does not give an error, but should not), but the abstract method in the interface can only be of the public type, and the default is the public abstract type.
5. Abstract classes can contain static methods, and interfaces cannot contain static methods
6. Both abstract classes and interfaces can contain static member variables, and the access type of static member variables in an abstract class can be arbitrary, but the variables defined in the interface can only be public static final types, and the public static final type is the default.
7. A class can implement multiple interfaces, but can inherit only one abstract class.
Let's talk about the difference between the two applications:
Interface is more in the system architecture design method to play a role, mainly used to define the communication contract between the modules. Abstract classes play a role in the implementation of code, can implement code reuse, for example, template method design pattern is a typical application of abstract class, it is assumed that all the servlet classes of a project to use the same way to determine permissions, log access logs and handle exceptions, you can define an abstract base class, Let all the servlets inherit this abstract base class, in the service method of the abstract base class, complete the permission judgment, record the access log and handle the exception code, in each subclass just complete their own business logic code, pseudo-code as follows:
Public abstract class Baseservlet extends HttpServlet
{
Public final void Service (HttpServletRequest request, httpservletresponse response) throws Ioexcetion,servletexception
{
Logging access logs
Permission to determine if (with permissions)
{
try {
Doservice (Request,response);
} catch (Excetpion e) {log exception Information}}}
protected abstract void Doservice (HttpServletRequest request, httpservletresponse response) throws Ioexcetion, Servletexception;
Note that access rights are defined as protected, which is both professional and rigorous, because it is used exclusively for subclasses}
public class MyServlet1 extends Baseservlet
{
protected void Doservice (HttpServletRequest request, httpservletresponse response) throws Ioexcetion,servletexception
{The specific business logic code that this servlet only handles}
}
The parent class method in the middle of the code is not certain, leaving the sub-class to do, the template method to design the pattern. Note: The idea of this problem is to first explain the basic concepts of abstract classes and interfaces, then compare the grammatical details of both, and finally the application of the two differences. Comparison of the two grammatical details of the hierarchy is: first from a class of construction methods, ordinary member variables and methods (including abstract methods), static variables and methods, inheritance and other 6 aspects to compare the answer, and then from the perspective of the third party of the answer, especially at the end of a typical example to show their deep technical foundation.
6, the abstract method can be static at the same time, whether it can be native at the same time, whether it can be synchronized at the same time?
Abstract methods can not be static, because the abstraction of the method is to be implemented by the quilt, and static and the sub-class is not related!
The native method means that the method is implemented with another platform-dependent programming language, and there is no problem with the implementation of the quilt class, so it cannot be abstract and cannot be mixed with abstract.
For example, the Fileoutputsteam class wants to deal with hardware, and the underlying implementation uses an operating system-related API implementation, for example, in the C language of windows, so, looking at the source code of the JDK, you can find The FileOutputStream open method is defined as follows: private native void Open (String name) throws FileNotFoundException; If we want to use Java to call the C language function written by others, we can not directly call, we need to follow the Java requirements to write a C language function, and our C-language function to call someone else's C language function. Because our C language function is written according to Java requirements, we this C language function can interface with Java, Java side docking way is to define the method corresponding to our C function, the corresponding method in Java does not need to write the specific code, but need to declare native before.
I don't think it's a problem with synchronized and abstract, because I've never seen it in my years of study and development, and I think synchronized should be useful in a specific way. Furthermore, the synchronization lock object used by the synchronized synchronization on the method is this, and the abstract method cannot determine what this is.
Java Basics Review (v)