Java frequently tested face question (II.)

Source: Internet
Author: User

Preface

Yesterday, the beginning of the "5 Questions a day interview" This kind of article, feel good, and some of the usual seemingly understood things, to figure out. Just like what is a virtual machine? This problem, it seems to know, but to tell a why, and confused forced, often back to see as the face of the test, try to use their own words out, see will not, will continue to see, understand understanding.

                

------WH

What does the method overlay (overriding) and method overloading (overloading) in Java mean?

Answer yourself:

Overrides: Also known as overrides, prerequisites: inheritance. Subclass a method in the parent class, copy it intact, write it in the method body

Note: Subclasses override methods of the parent class, access modifiers cannot be lower than the access modifiers of the parent method, and other return values, method names, parameters must be the same.

Overloading: Does not require inheritance, in its own class, and one of its own method name in the class, the parameters are different, there is no limit, this is the method of overloading.

    

Reference Answer:

Method overloads in Java occur when the same class has two or more methods with the same name but different parameters. In contrast, the method overrides the method that the subclass redefined the parent class. Method overrides must have the same method name, parameter list, and return type. The override may not restrict access to the methods it covers.


Self-evaluation:
Probably the main point has been answered, the language is a little deficient, not refined. A look is a rookie ~

1, Overloading: Method name is the same, parameters are different. modifier, return value does not matter.

2. Override: Method name, parameter, return value must be the same, modifier >= parent class method modifier

Note: Overrides are intended for polymorphic use, and the benefit of polymorphism is that it can make the code more maintainable. And overloading is for a method to choose more functions depending on the parameters.

           

     

Second, what is a constructor in Java? What is a constructor overload? What is a copy constructor?

Answer yourself:

Constructor: The method name is the same as the class name, and there is no return value. There is an parameterless constructor by default in each class.

Constructor overloading: In the constructor, the parameters are different, the other is the same, the function, do some initialization operation.

Copy constructor: This does not know ... You have not encountered this need to copy a constructor function.

Reference Answer:

When a new object is created, the constructor is called. Each class has a constructor function. The Java compiler creates a default constructor for this class in cases where the programmer does not provide a constructor for the class.
Constructor overloading and method overloading are similar in Java. You can create multiple constructors for a class. Each constructor must have its own unique argument list.
Java does not support copy constructors like in C + + because Java does not create a default copy constructor if you do not write the constructor yourself.

            

Self-evaluation:

Overall also answer point, copy constructor that concept, to C + + learning is not deep, actually did not learn, so do not know

1. The constructor is called when the new object is created. Each class has a constructor, with the constructor having the same name as the class name, no return value, and a default parameterless constructor for each class.

2, the overload of the constructor: similar to the overloading of the method, the only difference is the function is not the same, the constructor overload, add different parameters, to achieve the purpose of initializing the property.

3. Does Java support multiple inheritance?

Answer yourself:

This question, a bit silly, learned Java people know, certainly do not know how to inherit Ah, Java is only support single inheritance, multi-implementation. I remember supporting multiple inheritance in C + +.

        

Reference Answer:

Classes in Java do not support multiple inheritance, only single inheritance (that is, a class has only one parent). However, the interface in Java supports multiple inheritance, that is, a sub-interface can have multiple parent interfaces. (The function of the interface is to extend the function of the object, a sub-interface inherits multiple parent interfaces, indicating that the sub-interface extends several functions, when the class implements the interface, the class expands the corresponding function).

Self-evaluation:

Only to say the surface of the answer, can not be more in-depth explanation, the knowledge of their reserves is not enough, and I do not know that the interface can inherit more. This can be remembered.

1, Java is to support single inheritance, multi-implementation

2, implementation is said to be a class implementation of an interface, the interface only declares the method, but there is no method body, the interface can inherit more. An interface inherits multiple interfaces, indicating that the interface extends more functionality.

4. What is the difference between an interface and an abstract class?

Answer yourself:

Interface: Only the method is defined in the interface, there is no method body in the method, that is to say only what function is declared, and the implementation of the function is done by implementing the class of the interface. Keyword interface

Abstract class: For a class of things the same part of the extraction, you can have a specific method, you can also have the method of not writing method body. Features, keyword abstract

Difference:

Keyword Interface abstract

Interfaces are all declarations of methods, there is no method body in the abstract class can have a method body, you can only declare the method, you can also have member variables.

Interfaces need to implement implements abstract classes need to inherit extends

Implementing an interface requires implementing all the methods in the interface, inheriting the abstract class, overriding all the abstract methods in the abstract class, and, if not overriding, the class as an abstract class. An abstract class cannot be a new object.

Reference Answer:

Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, and the difference is that:
All methods in an interface are implicitly abstract. Abstract classes can contain both abstract and non-abstract methods.
A class can implement many interfaces, but only one abstract class is inherited
Classes can not implement all methods of abstract classes and interface declarations, and of course, in this case, the class must also be declared abstract.
An abstract class can implement an interface without providing an interface method implementation.
The variables declared in the Java interface are final by default. An abstract class can contain non-final variables.
The member functions in the Java interface are public by default. The member functions of an abstract class can be private,protected or public.
An 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.
You can also refer to the differences between abstract classes and interfaces in JDK8

Self-evaluation:

seem to know, actually do not understand the two of them the connection and the difference. There is also an understanding that the interface can have attributes, but each variable is declared as public static final. That's constant.

1, the interface is the top-level abstraction, the abstract class second.

2, all the variables in the interface City public static final and abstract classes can contain any type of variable interface all methods are abstract.

3, the interface is the top-level abstraction, so it only declares the method, and no method body, and abstract class can have method body method, also can not,

4, a class has an abstract method, then the class must be abstract class. And the class inheriting the class must override the abstract method,

5. Abstract can only modify classes and methods, and cannot modify attributes.

6, a method is modified by the abstract, then the method can only be declared, there is no method body.

            

Re-answer:

Differences: Three aspects, keywords, methods, member variables, others.

Keyword: interface interface abstract class

Interface is the top-level abstract abstract class followed by

All of the methods in an interface are urban abstractions, and interfaces can only be implemented, cannot be inherited, and methods in abstract classes can be abstract or not, abstract methods have no method body

member variables for an interface urban public static final modifies constants and abstract classes can be of various types.

      

V. What is value passing and reference passing?

Answer yourself:

Value passing: For the basic data type, it is to pass their values in

Reference delivery: For the reference data type, pass the address value,

Note: There is a saying that all of Java is a value pass, no reference to pass, in fact, the real value and reference value (address value).

Reference Answer:

A value pass is a basic variable, passing a copy of the variable, changing the copy without affecting the original variable.

A reference pass is generally for an object type variable, passing a copy of the object's address, not the original object itself.              Therefore, manipulating the reference object changes the original object. It is generally assumed that the delivery in Java is a value pass, which may be easier to understand. public void Add (int a) {int b = A;} This can be thought of as a value pass, A is a basic data type, he passed his value to B public void Add (Object obj) {object objtest = obj;} This can be seen as a pass-through, and obj is a reference data type that assigns the value of the address of the object in his stack to the heap to objtest. At this point, there are two references to one of the object objects in the heap. In fact, Java should only be passed by value. If it is a basic data type, the actual value is passed. If it is a reference data type, the address value of the reference is passed.

Self-evaluation:

The answer is not deep enough, just a superficial answer

1, the value is passed to the base reference type, if the value is assigned to another a variable, change the value of a variable, the value of the original variable will not change

2, the reference is passed to the reference base type, if the value of a variable is assigned to the B variable, change the value of the B variable, the value of a variable will also change, because it is passed an address, the value of an address is changed, all points to the address of the object will also change the value of the corresponding

Java frequently tested face question (II.)

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.