Access modifier parsing in Java

Source: Internet
Author: User

Access control modifier: public protected default Private
This static super

Final abstract

Where abstract this modifier is very temperamental, this modifier cannot coexist with static private final
For final, the final modified variable, the first time the variable is assigned, it cannot be assigned again, it can be understood as a constant, note the constant pool inside the memory space ... This is a special memory space inside the method area ...
The final modifier class, which means that the class is the final class, that is, cannot have subclasses, cannot be inherited, but any class, as long as it is not final decorated, can be inherited, can have subclasses, abstract classes also have subclasses, when a class is inherited, the class is final decorated members, is not cannot be inherited, cannot be overridden, the overriding method is that the subclass itself defines a function that is the same as the return value of the parent class function, the purpose of inheritance is generally to make the function of the subclass more powerful, otherwise the inheritance is meaningless, the method overrides can actually be considered as the upgrade of the parent class method (or function upgrade) ...

First look at the difference between this and the Super keyword

public class test{
public static void Main (string[] args) {
Cat c=new Cat (); System calls no parameter construct create object C
C.eat ();
}
}
Class animal{
public void Eat () {
System.out.println ("Animal Eats");}
}
Class Cat extends animal{
Public Cat () {
This.eat ();
}
public void Eat () {
Super.eat ();
System.out.println ("Cat eats Fish");}
}

This. Member method, which represents the member method that invokes the current class, which is, who, this. Member method this statement is in which class, which represents the member method that calls which class, super. The Member method represents the member method that invokes the parent class, this. Member method the statement is in which class, You call the Member method of the parent class of the class where ... To find out what the nature of the calling method is, see my analysis below

This.eat (); What the hell does that mean???? This means calling the Member method of the current class, this represents the body is not sure, who calls this method, this represents that object, call the member method, is to let this method play a role, and play a role in the method body of the statement, I said before the method insert, that is, where to call the method, The method body statement is inserted at the statement that invokes the method, so
This.eat (); The essence is a piece of code, the statement as a whole, the whole is to represent the method body statement, since it is a method statement, such as SYSTEM.OUT.PRINTLN (); This is a method statement, the method statement cannot exist independently, Method statements can only be relied on member methods, and member methods belong to the class, so we now understand that the this.eat (), where the statement is placed, just remember, this.eat () is a few statements, of course, the statement is placed in the member method (the main method is also a member method), Similarly, super.eat () is also placed in the member method, the nature of which is also a few independent statements, it must be placed in the method body, but
Super.eat () The class must have a parent class, otherwise super doesn't make sense, in a word, be sure to remember the method insert, remember what the nature of the method call ... Of course, if the method contains other methods, it is necessary to specifically analyze the matter, in short, if the method inside is a separate statement ... Be sure to pay attention to ...

Public Cat () {
This.eat ();
}

This is to put this.eat (); The parameterless constructor method in Cat class is also a method, but it differs from the member method of a class, the member method of a class can be called with an instance of the class or the class itself, but the function of the constructor is to provide initialization to the instance of the class, or simply, The function of the construction method is to generate the object, can give the object to provide the initial value (have the parameter constructs), but calls constructs the method the system, is not by the class to call, here, puts This.eat (), puts in constructs the method body, is no problem, because This.eat (); In fact, it is equivalent to System.out.println ("Cat eats fish"); Putting a separate statement in the method body is not controversial ... The same super.eat () is equivalent to an independent statement System.out.println ("Animal Eats"); it is no problem to put a separate statement in the Eat method body.

Cat c=new Cat ();
C.eat ();
It means to create an object with the null parameter construct of cat, and then assign the address of this object to C, and through c we can manipulate the members of this object, or more simply, the above statement is to create the object C, then use C to call the Eat method, and the same as above, C.eat (); The Eat method that calls the C object is actually equivalent to Super.eat (); System.out.println ("Cat eats fish"); This code, Super.eat (), is also a piece of code, so, as we said above, the essence of the invocation method is a code implementation function ...
Final output

Animals eat.
The cat Eats the fish
Animals eat.
The cat Eats the fish

This (...); Represents the invocation of this class constructor method, super (...); Represents a call to the parent class construction method.
This. member method, calling the member method of this class, or calling the Member method of the parent class (nearest principle, overriding or overwriting)
We certainly know that a member method requires a class or object to be called, but this is a member method, where this does not refer to a class, but to an object, the same super. Member method, where super is definitely referring to a parent class object ...
The correct idea is: we want to invoke the method, if it is non-static, then we have to create the object, not to create an object and then call the method, but to call a method, I go to create an object, invoke the essence of the method is: a block of code in the program, the advantage is to encapsulate a block of code, To implement this block of code, we do not have to write this code directly in the program, call the method directly, I would like to give an example:
public class test{
public static void Main (string[] args) {


}
}

We want to print "Your Sister", of course, we can add System.out.println ("Your Sister") directly in the main method;
However, we encapsulate this code in the method, because the calling method really works on the statement inside the curly braces of the method;
Do not be disturbed by the method name of these things ...
System.out.println ("Your Sister"); This is the statement that is really needed, so this statement is the execution statement of the method,

Write a method
public void print () {System.out.println ("Your Sister");

This method is encapsulated, so we call this method, call method can only be called by the class name or object invocation, but this method is non-static method, can only be called by the object, so we need the object, create the object needs the class, so we need to put this method in a class ... We can put it directly in the test class, so

public class test{
public static void Main (string[] args) {


}
public void print () {System.out.println ("Your Sister");

}

Note that the method is a side-by-side relationship, and the method cannot contain methods, because they are equal, and the main method is the same, since the Print method is in the test class, the method of the test class that calls the method uses the object of the test class, which is the object of the test class. Print method , but the calling method and the creation object (the system call constructor method) are separate blocks of code (which must be placed in the main method), so

public class test{
public static void Main (string[] args) {
Test t=new test ();
T.print ();
}
public void print () {System.out.println ("Your Sister");

}

Super. Member method, calling the parent class member method.
this. member variable, call the member variable of this class, or you can call the parent class member variable.
Super. Member variable, calling the parent class member variable.

The final modification method cannot be overridden, meaning that if a final method is defined in the parent class, the subclass can only inherit the method in its own right, and Java prohibits redefining the method in the subclass, and if you define it, the compilation will go wrong, so it is not possible to redefine the method in the subclass. At this time, there is no way to overwrite the override, or two methods who occupy the upper hand of the problem ...

Look at the code below.

public class test{
public static void Main (string[] args) {
Cat c=new Cat (); System calls no parameter construct create object C
C.eat ();
}
}
Class animal{
Public final void Eat () {
System.out.println ("Animal Eats");}
}
Class Cat extends animal{
Public Cat () {
This.eat ();
}

}

The Eat method of the animal class has been set to final, so the cat class will not be able to define the Eat method, because the cat class does not set its own member method, so there is only one member method in the Cat class, that is, the Eat method inherited from the animal ...

Of course, the final decorated class cannot be inherited, but the properties and methods of the final adornment can be inherited
For properties and methods to be inherited, you can use the private adornment, that is, members of the private adornment cannot inherit from the quilt class ...

Look at the following error code

public class test{
public static void Main (string[] args) {
Cat c=new Cat (); System calls no parameter construct create object C
C.eat ();
}
}
Class animal{
private void Eat () {
System.out.println ("Animal Eats");}
}
Class Cat extends animal{
}

In Amimal, the Eat method is set to private, the cat class simply cannot inherit to the Eat method, the cat class has no Eat method, C.eat (), and using C to invoke a eat method that does not exist, of course it will go wrong ...

And look at the code below.

public class test{
public static void Main (string[] args) {
Cat c=new Cat (); System calls no parameter construct create object C
C.eat ();
}
}
Class animal{
private void Eat () {
System.out.println ("Animal Eats");}
}
Class Cat extends animal{
public void Eat () {
System.out.println ("Cat eats Fish");}
}

This code can be compiled through, for what, because the cat class does not inherit the Eat method of the animal class at all, so the cat class has only one public void eat () {
System.out.println ("Cat eats Fish");} method, there is no overridden eat method inherited from the parent class ...

Then look at the following question code

public class test{
public static void Main (string[] args) {
Animal c=new Cat (); System calls no parameter construct create object C
C.eat ();
}
}
Class animal{
private void Eat () {
System.out.println ("Animal Eats");}
}
Class Cat extends animal{
public void Eat () {
System.out.println ("Cat eats Fish");}
}

Why is this code wrong??? Animal c=new Cat (); C.eat ();

Animal c=new Cat (); This is the parent class reference to the subclass object, that is, the transformation upward, why to write so, Animal C should have manipulated the members of the Animal class object, actually Animal c=new Cat (); This is written on the premise that the Cat class object has elements inherited from the Animal class, and Animal C can manipulate the elements that the cat class inherits from Animal, but the cat class does not inherit the Eat method of the Animal class, so c.eat (); To manipulate the inherited Eat method, Simply impossible, because the Eat method has no inheritance at all ...

Access modifier parsing in Java

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.