Java Core Foundation (2) Object oriented

Source: Internet
Author: User

Java Object-Oriented foundation

Summary of Interview questions (2)

1. What is the difference between overload and override (OverWrite)? Can the overload method change the type of the return value?

?? 1) overloaded overload is an overload of the method, which means that there can be more than one method with the same name in the same class, but the parameter lists of these methods are different, including different locations, different numbers, and different types.
?? 2) overriding override is a method override that means that a method in a subclass can be exactly the same as the name and parameters of a method in the parent class, and invoking the method from an instance object created by the subclass invokes the definition method in the subclass, which is equivalent to overwriting the exact same method defined in the parent class. This is also a representation of the polymorphism of object-oriented programming.
?? 3) When the override subclass overrides a method of the parent class, it can only throw fewer exceptions and smaller exceptions than the parent class, or throw a child exception for the exception thrown by the parent class. The subclass method can be accessed only in the same way as the parent class or larger than the parent class, and cannot be smaller. If the method of the parent class is private, then the subclass does not have an override limit, which is equivalent to adding a new method to the subclass. The return value of the overridden method must be consistent with the return of the overridden method;
?? 4) overload is not related to return value types and modifiers, and exceptions are overloads as long as the method name has the same form parameter.

2. What is the difference between a member variable (global variable) and a local variable?

?? 1) Define the location
???? A local variable is defined in a function, within a statement.
???? Member variables are defined in the class.
?? 2) The location in memory.
???? Local variables are stored in the stack memory.
???? Member variables are stored in heap memory.
?? 3) Initialization value
???? Local variables do not have default initialization values and must be assigned before they can be used.
???? Member variables have default initialization values.
?? 4) Life cycle
???? Local variables are released as soon as the area of the action ends.
???? A member variable, also known as an instance (object) variable, appears as the object appears. Released as the object is recycled.

3. What is the difference between a static variable and an instance variable?

?? The difference between the syntax definitions: Static variables are added with the static keyword, and the instance variable is not added before.
?? The difference when the program runs: instance variables belong to an object's properties, and an instance object must be created where the instance variable is allocated space before the instance variable can be used. Static variables are not part of an instance object, but belong to a class, so also known as class variables, as long as the program loads the class's bytecode, without creating any instance objects, static variables will be allocated space, static variables can be used. In summary, an instance variable must be created before it can be used by the object, and a static variable can be referenced directly using the class name.
?? 1) Life cycle
???? A member variable, also known as an instance variable, occurs as the object appears and disappears as the object disappears.
???? A static variable is also called a class variable, which occurs as the class is loaded and disappears as the class disappears.
?? 2) storage location in memory
???? Member variables exist in the object of the heap memory.
???? Static variables are stored in the static area of the method area.
?? 3) Characteristics of stored data
???? The data stored by the member variable is the unique data for the object.
???? The data stored by the static variable is the shared data of the object.
?? 4) Call mode
???? Member variable that can only be called by the object.
???? Static variables, which can be called by the object or called by the class name

4, please say the scope of public, private, protected, and not write the difference between

Description: If no access modifier was written above the decorated element, the friendly is represented.

5. Can the constructor Constructor be inherited? Can I be overload by override?

The constructor constructor cannot be inherited and therefore cannot override override, but can be overloaded with overload.

6, final, finally, finalize the difference

?? Final is used to declare properties, methods, and classes, respectively, that the property is immutable, that the method is not overridden, and that the class is not inheritable.
Internal classes to access local variables, local variables must be defined as final types.
?? Finally is part of the exception-handling statement structure, which indicates that it is always executed.
?? Finalize is a method of the object class that, when executed by the garbage collector, calls this method of the reclaimed object, and can override this method to provide garbage collection of other resource recycles, such as closing the file. (Before garbage collection, which is used to activate the object, if it cannot be activated, it is recycled) the JVM does not guarantee that this method is always called.

7. What is the difference between "= =" and Equals method?

?? The = = operator is specifically used to compare the values of two variables, that is, whether the value stored in the memory used to compare the variables is the same, to compare two basic types of data or two reference variables equal, only with the = = operator. If you want to compare whether the two variables point to the same object, that is, to see whether the two variables correspond to the values in memory are equal, this time you need to use the = = operator to compare.
?? The equals method is used to compare the contents of two separate objects, as compared to two people whose looks are the same, compared to the two objects that are independent of each other.

8. Is it possible to make a call to a non-static method from within a static method?

?? No. Because the non-static method is to be associated with an object, you must create an object before you can make a method call on the object, and the static method call does not need to create the object, which can be called directly. That is, when a static method is called, there may not be any instance objects created, and if a call to a non-static method is emitted from a static method, which object does that non-static method relate to? This logic cannot be set up, so a static method cannot be called inside of a non-static method.

9. Can interfaces inherit interfaces? Can an abstract class implement (implements) interfaces? Can an abstract class inherit a concrete class (concrete Class)? Is there a static main method in the abstract class?

Interfaces can inherit interfaces. Abstract classes can implement (implements) interfaces, and abstract classes can inherit concrete classes. There can be static main methods in an abstract class.

10. What is the difference between abstract class and interface (interface)?

abstract class:
?? (1) A class that contains an abstract modifier is an abstract class, an instance object that the abstract class cannot create in other words, it cannot be new.
?? (2) A class containing an abstract method must be defined as an abstract class, and an abstract class can have a common method.
?? (3) The subclass of an abstract class must all overwrite all of the abstract methods of the abstract class, so there can be no abstract constructor method 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.
?? (4) Abstract can modify the class, modify the method, cannot modify the property
interface:
?? (1) The interface (interface) can be said to be a special case of abstract class, the interface is more pure abstract class, there can be no method implementation, there can only be a method of declaration.
?? (2) 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.
?? (3) The implementation class must implement all the method declarations in all interfaces;
?? (4) There is no constructor in the interface, that is, the interface has no object;
?? (5) interface is a multi-implementation, that is, a class can implement multiple interfaces, separated by commas;
compares the syntax differences:
?? 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 classes can contain static methods, and interfaces cannot contain static methods
?? 5. 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.
?? 6. A class can implement multiple interfaces, but can inherit only one abstract class. The difference between the
and the application:
?? Interfaces are more useful in system architecture design methods, and interfaces are used to define specifications. It is used primarily to define communication contracts between modules. Abstract classes play a role in the implementation of code, can realize the reuse of code, the realization of a value called template strategy.

11. What is the mechanism for polymorphism in Java?

Polymorphism can be divided into:
?? 1. Compiler polymorphism: mainly in overloading, the system can determine which version of the overloaded function is called at compile time.
?? 2. Operation polymorphism: mainly embodied in the inheritance of OO design, the object of the subclass is also the object of the parent class, that is, the child class object can be used as the parent class object, the object variable of the parent class can point to the subclass object. Therefore, a method call made from a parent class might be implemented in the parent class of a method, or it might be an implementation in a subclass, determined by the type of object at run time. 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

12. What is an internal class? What is the difference between Static Nested class and inner class?

?? (1) An inner class is a class defined inside a class, and the inner class is divided into static inner classes, non-static inner classes, local inner classes, and anonymous inner classes.
?? (2) A non-static inner class can access the members of an external class unconditionally, not the same name as its own external class, but can be the same class as the outer class, and the non-static inner class cannot have static modified variables.
?? (3) A static inner class cannot access a non-static property of an external class, and an external class cannot access a member of a static inner class, but it can use the class masterpiece of a static inner class for the caller.
?? (4) The inner class can be instantiated by the new external class (). New inner Class () Inner in=new Outer (). New Inner ();
An inner class can be inherited by another class to continue its class needs to write a constructor method and invoke the external class manually. super ();

publicclass SubClass extends Out.In{            publicSubClass(Out out){                out.super();            }        }
13, the difference between Integer and int

?? (1) int is one of the 8 raw data types provided by Java. Java provides encapsulation classes for each primitive type, and Integer is the wrapper class provided by Java for Int. The default value for int is 0, and the default value for integer is null, which means that integer distinguishes between unassigned and value 0, and int cannot express an unassigned condition.
?? (2) In JSP development, the default of Integer is null, so when displayed in a text box with an El expression, the value is a blank string, and the default value of int is 0, so when the El expression is displayed in the text box, the result is 0, so int does not work as a web-tier form data Type.
?? (3) in Hibernate, if the OID is defined as an Integer type, hibernate can determine whether an object is temporary based on whether its value is null or not, and if the OID is defined for the int type, it is also required to set its UN in the HBM mapping file. The Saved-value property is 0.
?? (4) In addition, the integer provides multiple operations that are related to integers, such as converting a string to an integer, and also defining constants that represent the maximum and minimum values of integers.

14, when writing the Clone () method, usually have a line of code, what is it?

Must implement Cloneable interface override Clone method

protectedclonethrows CloneNotSupportedException {        returnsuper.clone();    }
15. Can I inherit the String class?

?? The string class is final decorated. Therefore, this class cannot be inherited and cannot be modified. In order to improve efficiency and save space, we should use the StringBuffer class

16. String s = new string ("xyz"); how many string Object did you create? What's the difference between the two?

?? Two or one, "xyz" corresponds to an object, which is placed in a string constant buffer, and the constant "XYZ" is the one in the buffer, no matter how many times it occurs. The new string, each time it is written, creates an object that creates a new string object based on the contents of that constant "XYZ" object. If you've used ' xyz ' before, that means it won't create "xyz" yourself, just take it from the buffer.

Java Core Foundation (2) Object oriented

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.