1. Object-oriented and process-oriented differences
Object-oriented data and methods of operation of the data are put together as an interdependent whole, that is, the object. The common object is abstracted from similar objects , that is, most of the data in classes and classes can only be handled by the methods of this class. The class is connected to the outside world through a simple external interface , and the object communicates with the object through the message . The program flow is determined by the user in use.
Process-oriented is an event-centric development approach, that is, from the top down sequential execution, the gradual refinement, its program structure is divided into a number of basic modules according to function, these modules form a tree structure, the relationship between the modules is relatively simple, functionally independent, each module is generally ordered from the internal, The selection and circulation of 3 basic structures, the implementation of a modular approach is the use of subroutines, and the program process is written in the process has been decided.
Object-oriented and process-oriented are mainly different in the following aspects:
1) different starting points. the object-oriented emphasis maps the essentials of the problem domain directly to the interface between the object and the object, and the process emphasizes the abstraction and modularity of the process.
2) hierarchical logical relationships are different. the hierarchical structure of class is used to embody the inheritance and development of class, and the hierarchy structure of process-oriented module summarizes the relationship and function between modules and modules.
3) data processing methods and control procedures are different. object modification of object-oriented method can only be done by its own member function, and the way of controlling control is to activate and run the program by "event-driven". And the process-oriented method is to process the data directly through the program, in the control mode is according to the design call or return program, the module exists between the control and is controlled, called and called the relationship.
2. Object-oriented features
1) abstract. Abstraction is about ignoring aspects of a topic that are not related to the current goal, so that you can more fully focus on the aspects that are relevant to the current goal. Abstractions do not intend to understand all of the problems, but simply select one part of them, temporarily without some detail. Abstraction consists of two aspects: One is process abstraction, the other is data abstraction.
2) inheritance. inheritance is a hierarchical model of a junction class and allows and encourages the reuse of classes, which provides a way to articulate commonalities. subclasses can inherit methods and instance variables from the parent class, and subclasses can modify or add new methods to make them more suitable for special needs.
3) package. encapsulation refers to the abstraction of objective things into classes, each of which protects its own data and methods. classes can put their own data and methods to allow only trusted classes or objects to operate, to the untrusted information hiding.
4) polymorphic. Polymorphism means that objects of different classes are allowed to respond to unified messaging.
3. Differences in composition and inheritance
Composition and inheritance are two ways to reuse code in an object-oriented manner. A combination is an object in a new class that creates an existing class and reuses the functionality of an existing class. Inheritance is one of the main object-oriented features that allows designers to define an implementation of a class based on an instance of another class. Both composition and inheritance allow child objects to be set in a new class, except that the combination is explicit and inheritance is implicit.
Class verhicle{} //Vehicle class tire{} //Tyre class Car extends verhicle{ //is-a relationship Private Tiret = new Tire (); C12/>//has-a Relationship}
Attention:
1) unless the two class is a "is-a" relationship, do not easily use inheritance, not simply to implement code reuse and use inheritance, resulting in the coupling between classes to spend high, affecting the difficulty of program maintenance.
2) The model of the strategy in the design mode shows that if there is no "is-a" relationship between the classes, it is more extensible to adopt the interface and combination method than the inheritance.
4. Multi-state Implementation mechanism
(See compile-time polymorphism and run-time polymorphism)
5. The similarities and differences between abstract class and interface (interface)
as long as a class containing an abstract method must be declared as an abstract class, the abstract class can declare the existence of the method without implementing it, and the method declared as abstract cannot contain the method body. Subclasses of an abstract class provide concrete implementations for all abstract methods in the parent class, or they are abstract classes.
interfaces can be considered variants of abstract classes. All of the methods in the interface are abstract and can be used to implement multiple inheritance concisely through interfaces. the member variable in the interface defaults to the public static final type.
interface to the same point as the abstract class:
1) can not be instantiated
2) The implementation class of an interface or a subclass of an abstract class can only be instantiated by implementing a method in an interface or abstract class.
Different points:
1) The interface is only defined, its methods can not be implemented in the interface, only the implementation of the interface class to implement the method defined in the interface, and the abstract class can have a definition and implementation, that its method can be implemented in the abstract class,
2) interfaces need to be implemented (implements), abstract classes need to inherit (extends). A class implements multiple interfaces, but only one abstract class can be inherited.
3) The interface emphasizes the realization of the specific function, the design concept is "has-a" relationship, while the abstract class emphasizes the relationship, the design concept is "is-a" relationship
4) The interface member methods can only be modified bypublic, abstract classes may contain their own data member variables and non-abstract member methods.
5) The interface is used to achieve more commonly used functions, to facilitate future maintenance or to add the deletion method, and the abstract class is more inclined to act as a public class role, not to be used to re-modify the code inside later
6. Inner class
in Java, a class can be defined inside another class, and the class inside the class is called an inner class, outside of which is called an outer class. In this case, the inner class can be considered a member of an external class.
There are 4 types of internal classes in Java, respectively, as follows:
Class outerclass{ Staticclass innerclass1{}/ /static inner class class innerclass2{} //Member internal classes public Voidmemberfunction () { class innerclass3{} //local inner class } publicoutclass{ //External class AddListener (new Listener) {} //Anonymous inner class, generally applied to GUI }}
Note:
Static members cannot be defined in non-static inner classes, static inner classes cannot access non-static members of external classes, and non-static inner classes are instantiated only after an external class is instantiated.
7. Get the class name of the parent class
The Java language provides a way to get the class name: GetClass (). GetName (), developers can call this method to get the class name.
The GetClass () method is defined in the object class as final and Naticve, and the subclass cannot overwrite the method, so only the run-time class of this object can be returned. To get the name of the parent class in the subclass, you can use the GetClass (). Getsuperclass (). GetName () through the reflection mechanism of java.
8. The difference between this and super
In the Java language,T-his is used to point to the current instance object , One of its very important functions is to distinguish the object's member variables from the method's parameters.
Super can be used to access methods or member variables of the parent class. When a method or member variable of a subclass has the same name as the parent class, the method or member variable of the parent class is overwritten, and the method or member variable to access the parent class can only be accessed by the Super keyword.
Reference documents:
Java Programmer interview written book
The object-oriented technology of Java basic Knowledge (ii)