Abstract: Java knowledge points such as selection, interface, enumeration 30 questions, is more basic, I hope we learn progress together.

Sorted out some JAVA language in the class, interface, enumeration and other aspects of knowledge points and we often encountered problems. I hope it helps.

Q: What visibility does each modifier represent? Public is available to all classes in the same package. Protect is available to subclasses in the same package. Private is available to all classes in the same package

Q: Can external classes be modified with private or Protect? A: No, only public or package access. Inner classes do.

Explain the role of the following final members Q: Final members? A: If it is A basic type, the value cannot be changed. In the case of an object, the reference to the object cannot be changed, but the contents of the reference can be changed.

Q: Final parameter? A: Parameters are immutable and can only be read but cannot be modified

Q: final method A: method cannot be overridden by subclasses.

Q: final class A: This class cannot be inherited.

Q: Can final local variables be passed in as non-final arguments? Will it compile with errors?

public static void main(String[] args){
	final A a = new A();
	changeA(a);
}
public void changeA(A a) {
	// change A...
}
Copy the code

A: It can be passed in as A non-final argument without compiling an error.

Q: What is the difference between overloading and overwriting? A: Overloading is the same method name but different parameters. Overrides are methods that override the parent class if the method parameters are the same.

Q: If a subclass overrides a method in its parent class, can a subclass call a method with the same name in its parent class? A: yes (C++ cannot call an overloaded method of the same name in A parent class).

Q: How can subclasses avoid accidentally overwriting a method of their parent class? (F (int, int) = f(int, int) = f(int, int) = f(int, int) A: Add the @override keyword.

Q: Can a member variable of a parent class be overridden?

class A{
	public String name = "A";
}
class B extends A{
	public String name = "B";
}
public static void main{
	A a = new B();
   System.out.println(a.name);
}
Copy the code

A: Outputs A. Note that member variables are not polymorphic, so if you define A and assign B, the output is still the member of A. If it’s a method that’s being overridden, it’s going to use the method in B.

Q: What is an inner class? Can an inner class access members of an outer class? A: Inner class concept:

class A { class B{ ... }}Copy the code

B is the inner class of A. B has access to all members of A

Q: THERE is an inner class C in A, and there is an inner class C in subclass B of A. Does C in B overwrite C in A? A: No, because it is called through B.C or A.C, there is A namespace relationship.

Q: Can static members be defined in an inner class?

class A { class B{ static int b; . }}Copy the code

A: No. Unless you add static to class B to make a static class

Q: What are anonymous classes? Can anonymous classes access external variables or objects? A: Anonymous class concept:

Return new A(constructor argument){{constructor content} class definition}Copy the code

Anonymous classes that use external objects must be defined as final.

Q: What is a nested class? Can you access members of an external class? A:

class A {
   static int sa;
   int a;
	static class B{}
}
Copy the code

B can access only static member SA of A, but not A.

§ interface

Classes are single inherited, and interfaces can be multiple inherited

Q: If you define a member variable in an interface, what is the default modifier for the member? A: Public static final

Q: What are the default modifiers for each method in the interface? A: public abstract

Q: Can implementation methods be defined in the interface? A: java8 and later versions are ok. The introduction of the default keyword, in the interface with the default keyword to modify the interface, you can implement the interface in the interface.

§ enumeration

Q: Can enum be inherited? Something like this:

enum A extend B{
...
}
Copy the code

A: No. Enum identifiers are themselves processed by the compiler and inherit from the enum class, and Java does not support multiple inheritance. However, implementation interfaces are supported

Q: Is default required for switch(enum)? A: I don’t think so.

Q: Is the values() method implemented in the Enum base class? A: No implementation, values method is added by compiler. Therefore, values () cannot be called for objects that are pulled from the List.

Q: What is the default modifier for enumerations in enum? A: the static final

§ Static dispatch and dynamic dispatch

Q: What is the output next, and what is the dispatch?

void test() { Father father = new Son(); // print(father); } void print(Father father) { System.out.println("this is father"); } void print(Son son) { System.out.println("this is son"); }Copy the code

A: This is father. Belongs to static dispatch. The concept of static dispatch: the compiler can determine which method to call. In this case, the two print methods are overloaded. The definition type of the input parameter immediately determines which static dispatch to call, depending on the static type

  • Static typing concepts: types that are written to Java files at compile time and are immediately visible such as A A = factory.create (args); So the A on the left is static, and the type on the right is indeterminate depending on the runtime.

Q: The progression of the previous question:

public class Overload { private static void sayHello(char arg){ System.out.println("hello char"); } private static void sayHello(Object arg){ System.out.println("hello Object"); } private static void sayHello(int arg){ System.out.println("hello int"); } private static void sayHello(long arg){ System.out.println("hello long"); } public static void main(String[] args) {sayHello('a'); }}Copy the code

Output what? A: Print hello char Because ‘A’ is A char data (that is, the static type is CHAR), an overloaded method of type char is selected. If you comment out the sayHello(char arg) method, it will print hello int because ‘a’ can stand for the number 97 as well as a string. So when there is no best way to overload sayHello(char arg), the second-best (second-priority) method overload sayHello(int arg) is selected.

Summary: When there is no best way to reload, the second-highest priority method is used to reload, and so on. Char >int> Long >float> Double >Character>Serializable>Object>… Among them… Is a variable-length argument, which is treated as an array element. Variable length parameters have the lowest overload priority. Since the conversion from char to byte or short is unsafe, methods of byte or short are not overloaded and therefore not in the priority list.

Q: What is output below, which dispatch belongs to:

void test() { Father father = new Son(); father.name(); } class Son extends Father { void name(){ System.out.println("son"); } } class Father { void name(){ System.out.println("father"); }}Copy the code

A: Outputs son, which belongs to dynamic dispatch. Runtime determines which method to call based on the specific object it points to

Q:

  • Is static dispatch single dispatch or multiple dispatch?
  • Is dynamic dispatch single dispatch or multiple dispatch? A: Static dispatch is multiple dispatch. Dynamic dispatch is single dispatch. Multiple dispatch concept: The type of the caller is considered as well as the type of the parameter when dispatching. Single dispatch considers only the type of caller.

Dynamic dispatch principle:

Q: What do class methods look like in a class file? A symbolic reference or a direct reference? A: In the class file, all methods defined are just symbolic references, that is, just symbols, and we know the name of the method, but we don’t know the actual instruction to run the method. The address symbol references are as follows, but I’m showing you that when class_info is A symbolic reference to the class, we actually have method_info which is A reference to the method:

Then the method is stored in the class file like this, first with a method_count number, then with the method.

The instruction address of the method is not known at this point. Unless you move from a symbolic reference to a direct reference.

Q: When does a symbolic reference to a method become a direct reference in the actual method area? A: The parsing phase of class loading replaces symbolic references to methods that meet “compile-time knowable, run-time immutable” with direct references to the method area and is not deferred until runtime.

  • structure
  • private
  • static
  • All four of these methods are recognized when the class is loaded and converted into direct references to the method area (i.e., instruction addresses are known instead of bytecode symbols)

Q: Dynamic dispatch (polymorphic), how does the virtual machine decide which method to call? How can he determine whether Son’s do or father’s do is called?

int a = 1;
Father f = new Son()
f.do(a);
Copy the code

A: First, through static dispatch, he knows that the f(int A) method must be used, but he doesn’t know which class of do(int A) method to use. When you execute f.dot (a), an object reference is placed on the operand stack

The virtual machine instruction executing f will find the class of its actual type through this object reference

He looks in the actual class to see if the method is implemented, and if there is a definition of the method in the class file

If it doesn’t, it goes to the parent class, whose relationship is found in the class file

If there is no parent class, keep looking until you find one that does.

This article is shared by Huawei Cloud community “Java knowledge points problem selection, interface, enumeration”.

Click to follow, the first time to learn about Huawei cloud fresh technology ~