Two ways to use Java keyword Default
Actually, I haven't paid attention to it beforedefault
The existence of this keyword has recently been revisited by the book "Java Language Programming", and I want to summarize some commonly used modifiers. I accidentally found... well... it's like this.
Two ways to use:
According to my observation, in general,default
There are not many uses, only two:
- exist
switch
Used when using a statementdefault
- Used when defining interfaces
default
To modify the specific method
The first method of use
Code:
int day = 8; String dayString; switch (day) { case 1: dayString = "Monday"; break; case 2: dayString = "Tuesday"; break; case 3: dayString = "Wednesday"; break; case 4: dayString = "Thursday"; break; case 5: dayString = "Friday"; break; case 6: dayString = "Saturday"; break; //If the case does not have a matching value, it must be Sunday default: dayString = "Sunday"; break; } (dayString);
Summarize:
- Use is simpler, that is, when the value in the case does not match the key in the switch, execute the method in default.
- In the example here, the key is 8, so the key does not match the values of all cases, so the output Sunday.
The second method of use
Interface definition IntefercaeDemo, defines an interface, which contains two specific methods and an abstract method.
IntefercaeDemo .java
public interface IntefercaeDemo { //Specific method default void showDefault(){ ("this is showDefault method"); } static void showStatic(){ ("this is showStatic method"); } //Abstract method without implementation void sayHi(); }
- LearnDefault implements the IntefercaeDemo interface.
LearnDefault .java
public class LearnDefault implements IntefercaeDemo{ //Implement abstract method @Override public void sayHi() { ("this is sayHi mehtod"); } public static void main(String[] args) { //Specific methods for modifying static in the interface (); //Instantiate the class that implements IntefercaeDemo LearnDefault learnDefault = new LearnDefault(); //The specific method modified by Default can be called by referencing variables (); } }
illustrate:
- In order to strengthen the interface's capabilities in JDK 1.8, there are specific methods for the interface, provided that the methods need to be modified by default or static keywords.
Summarize:
- The purpose of the default modification is to allow the interface to have specific methods and include some default method implementations inside the interface.
- The default method is the default method of the interface. As long as the class that implements the interface has such a default method, the default method can also be rewritten.
- We can imagine a scenario where classes that implement a certain interface have the same function. If, like the previous version of Java 8, each implementation class needs to write a piece of duplicate code to implement that function, which seems unnecessary. This is the meaning of existence.
Notice:
In addition, since the modifier default of the interface is mentioned here, we should pay attention to one thing: if a class implements two interfaces (which can be regarded as "multiple inheritance"), and both interfaces contain a default method with the same name at the same time, what will happen?
In this case, the compiler will report an error and get a compiler error because the compiler does not know which one should be selected among the two default methods of the same name, thus creating ambiguity.
Supplement: Where it is easy to confuse
The keyword default here should not be confused with the default when we usually define methods in a class without adding any access modifiers. Their meanings are different.
public class Demo{ //There is no access modifier to modify, so the default is (default) String name; void show(){} }
Here (default) refers to a scenario where members in the class are not modified by the access modifier, so the case where the (default) situation is that members in the case of default can only be accessed in the package where the Demo class is located.
The default keyword mentioned in this blog post is a real keyword and needs to be explicitly declared. Currently, there are only two usages mentioned. Has nothing to do with (default) scene
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.