The object-oriented Java interview preparation

Source: Internet
Author: User
Tags set set

Object oriented

The following lists the advantages of object-oriented Software development: (1) Code development is modular, easier to maintain and modify. (2) Code reuse. (3) Enhance the reliability and flexibility of the code. (4) To increase the understanding of the code. Object-oriented programming has many important features, such as: encapsulation, inheritance, polymorphism, and abstraction.

Three features and meanings of Java object-oriented

I. Inheritance: 1. Concept: Inheritance derives new classes from existing classes, and new classes can absorb data properties and behaviors of existing classes, and can extend new capabilities 2. Benefits: Improve code reuse and shorten development cycles. Ii. polymorphism: 1. Concept: polymorphism (polymorphism) literally means "multiple states, that is, the same entity has multiple forms at the same time." The general manifestation is that the same type behaves differently under different conditions in the process of running the program. Polymorphism, also known as dynamic binding, is usually the time to determine the specific execution object of a method, also known as dynamic delegation. 2. Benefits: 1) separate interfaces and implementations, improve the organization and readability of code, and create extensible programs. 2) Eliminate the coupling between types. Allows multiple types to be treated as the same type. 3) The invocation of a polymorphic method allows for multiple representations of the three, encapsulated: 1. The concept is to combine the object's attributes and operations (or services) into a single whole, and hide as much of the internal implementation details of the object as possible. 2. Benefits: (1) Hide the information and implement the details. Make it impossible for client programmers to touch the parts they should not touch. (2) Allows designers to change the way they work inside a class without worrying about affecting the client programmer.

Overload and Override

overload is overloaded meaning, override is the meaning of overrides, that is, rewriting. overloaded overload means that the same class can have more than one method with the same name, but these methods have different parameter lists (that is, the number of arguments or the type differs). override override means that the method in the subclass can be exactly the same as the name and parameter of one of the methods in the parent class , When this method is called by the instance object created by the subclass, the definition method in the subclass is called, which is equivalent to overwriting the exact same method defined in the parent class, which is also a representation of the polymorphism of object-oriented programming. When a subclass overrides a method of a parent class, it can only throw fewer exceptions than the parent class, or a child exception that throws an exception thrown by the parent class , because subclasses can solve some problems with the parent class and cannot have more problems than the parent class. The subclass method can be accessed only larger than the parent class, and cannot be smaller . If the method of the parent class is private, the subclass does not have a limit of overrides , which is equivalent to adding a new method to the subclass. As to whether the Overloaded method can change the type of return value, it depends on what you want to ask. The subject is very vague. If several Overloaded methods have different parameter lists, their type of returnees can of course be different. But I guess the question you want to ask is: if the parameter list of the two methods is exactly the same, can you make them different from the return values to implement the overloaded overload. This is not possible, we can use contradiction to illustrate this problem, because we sometimes call a method can also not define the return result variable, that is, do not care about its return results, for example, when we call the Map.Remove (key) method, although the Remove method has a return value, However, we usually do not define a variable to receive the return result, when it is assumed that the class has two names and the parameter list is exactly the same method, only the return type is different, Java will not be able to determine the programmer's bottom is to invoke which method, because it can not be judged by the return result type.

Override can be translated as an overlay, literally knowing that it is covering a method and rewriting it to achieve different effects. The most familiar overlay for us is the implementation of the interface method, which is generally just a declaration of the method in the interface, and we need to implement all the methods of the interface declaration when we implement it. In addition to this typical usage, we may also overwrite methods in the parent class with the subclass in the inheritance. in the overlay to pay attention to the following points : 1, the mark of the method of coverage must be matched with the mark of the method covered, in order to reach the effect of coverage; 2. The return value of the overridden method must be the same as the return of the overridden method, or its subclass (covariant return type); 3. The exception that is thrown by the overridden method must be the same as the exception that is thrown by the overridden method, or its subclass; 4. The overridden method cannot be private, otherwise only a new method is defined in its subclass. It is not overwritten.

Overload may be more familiar to us, can be translated as overloaded, it means that we can define some of the same name, by defining different input parameters to differentiate these methods, and then call, the VM will be based on different parameter styles, to choose the appropriate method of execution. The following points are to be noted when using overloading: 1. You can only pass different parameter styles when using overloads. For example, different parameter types, different number of parameters, different parameter order (of course, several parameter types within the same method must be not the same, such as can be Fun (int,float), but not fun (int,int)); 2, cannot pass access, return type , throws the exception to overload , 3, the method's exception type and the number does not have the influence to the overload ;

The difference between an interface and an abstract class

Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, except that: 1, all the methods in the interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods. 2, a class can implement many interfaces, but only one abstract class 3 is inherited , and if a class is to implement an interface, it must implement all the methods of the interface declaration. However, a class can not implement all methods of an abstract class declaration, and of course, in this case, the class must also be declared abstract. 4, an abstract class can implement an interface without providing an interface method implementation. The variables declared in the 5,java interface are final by default. An abstract class can contain non-final variables. The member functions in the 6,java interface are public by default. The member functions of an abstract class can be private,protected or public. 7, the interface is absolutely abstract and cannot be instantiated. An abstract class can also not be instantiated, but it can be called if it contains the main method.

Interface

Interface is important, in order to illustrate the situation, here is a bit verbose: (1) The interface is used to describe all the services provided by the system, so the member constants and methods in the interfacemust be public (publicly) typeTo ensure that external users can access them; (2) The interface simply describes what the system can do, but does not specify how it should be done, so the methods in the interfaceare abstract methods; (3) The interface does not involve details related to any specific instance, so the interface has no construction method, cannot be instantiated, there is no instance variable,static variable only; (4) The variables in the interface are all implementation classesin common, since the common, is definitely the same thing, because the change of things can not be counted in common. Sovariables are immutable (final) types, which are constants.。 (5) The variable cannot be defined in the interface. If an interface can define a variable, but the method in the interface is abstract, the property cannot be modified by behavior in the interface. Some people will say, no relationship, you can modify the properties of an interface by implementing the behavior of the interface's objects. This is of course not a problem, but consider such a situation. If interface A has a static variable A that has public access permissions. According to Java semantics, we can not access the variable a by implementing the object of the interface, through a.a = xxx; You can change the value of variable a in the interface. As can be done in an abstract class,then all objects that implement interface A will automatically have the value of this changed a, which means that a place has changed a, and all of these objects have the same value .。 What is the difference between this and the abstract class, how to embody the higher level of abstraction of the interface, how to embody the unified protocol provided by the interface, that also interface this abstraction to do? Therefore, the interface can not appear in the variable, if there are variables, and the interface provided by the unified abstraction of the idea is inconsistent. Therefore, the attribute in the interface must be constant, can only be read and cannot be changed, so as to provide a uniform property for the object that implements the interface. In layman's words, what you think is to change is put in your own implementation, not in the interface, interface is just a kind of things of the properties and behavior of a higher level of abstraction. Closed to the modification, open to extensions (different implementations implements), interface isThe principle of closureA manifestation of. So:the method of the interface is default to public abstract, you cannot define a variable in an interface, you can only define a constant (plus the final decoration becomes a constant).so the properties of the interface default to the public static final constant, and the initial value must be assignedNote: Final and abstract cannot occur at the same time.

Two object values are the same (x.equals (y) = = true), but can have different hash code, this sentence right?

A: No, if two objects x and y satisfy x.equals (y) = = True, their hash code (hash code) should be the same. Java for the Eqauls method and Hashcode method is this: (1) If two objects are the same (the Equals method returns True), then their hashcode value must be the same, (2) if two objects Hashco De same, they are not necessarily the same . Of course, you may not have to do as required, but if you violate the above principles you will find that when using containers, the same objects can appear in the set set, while the efficiency of adding new elements is greatly reduced (for systems using hash storage, if the hash code conflicts frequently, the access performance will be drastically reduced). (3) If the information used for the comparison operation of the Equals method of an object is not modified, the Hashcode method must consistently return the same integer if it is called multiple times on the same object .

Add: A lot of Java programs know about Equals and hashcode methods, but a lot of people just know, in Joshua Bloch's masterpiece effective Java (many software companies, effective Java, Java programming Thought "and" refactoring: Improving Existing code quality "is a Java programmer must read books, if you have not seen, then quickly go to the Amazon to buy a copy of the method is described in this way: first the Equals method must satisfy the reflexivity (x.equals (x) must return True), When symmetry (x.equals (Y) returns True, Y.equals (x) must also return true), transitivity (X.equals (y) and y.equals (z) all return True when X.equals (z) must also return true) and consistency (when the object information referenced by x and Y is not modified, multiple calls to X.equals (y) should get the same return value), and a reference to any non-null value x,x.equals (NULL) must return FALSE. The tips for achieving a high-quality equals method include:

**使用 == 操作符检查“参数是否为这个对象的引用”**;**使用 instanceof 操作符检查“参数是否为正确的类型”**;**对于类中的关键属性,检查参数传入对象的属性是否与之相匹配**;**编写完 equals 方法后,问自己它是否满足对称性、传递性、一致性**;**重写 equals 时总是要重写 hashCode**;**不要将 equals 方法参数中的 Object 对象替换为其他的类型,在重写时不要忘掉 @Override 注解**。
is AOP and OOP,IOC and DI

1) Object-oriented programming (object Oriented Programming,oop, OO programming) is a computer programming architecture. AOP is the continuation of OOP and is the abbreviation of Aspect oriented programming, meaning aspect-oriented programming. Separating the generic requirements function from the unrelated classes, while allowing many classes to share a behavior, once the behavior changes, there is no need to modify many classes, just modify this behavior. AOP is this decentralized programming approach that encapsulates "focus" in "facets"

2) control reversal IOC (inversion of control) controls refers to dependencies between program-related classes. In traditional conceptual design, it is usually the caller who creates an instance of the callee, in Spring, the work of creating the callee is no longer done by the caller, but by the The Spring container completes and the dependencies are reversed, called control reversals, for better extensibility and good maintainability . Dependency Injection (Dependency injection) The work of creating the callee is done by the Spring container and then injected into the caller, hence also called dependency injection. Control inversion and dependency injection are the same concept .

The object-oriented Java interview preparation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.