1, the domain of the hidden
The code is as follows:
class Base { public String className = "base";} class extends Base { private String className = "Derived";} Public class Privatematter { publicstaticvoid main (string[] args) { System.out.println (new Derived (). className); }}
Let's guess what the output is? Or does the compilation not pass?
There are variable classname in both the parent and subclass derived, although base has a common domain classname, but the domain is not inherited into the derived class because it is hidden by derived in classname. And because the classname in derived is private, it does not have access rights, resulting in a compilation error.
So how do we get to access the public variable classname of base? You'll definitely think of coercion type conversions, and that's what the truth is!
The code is as follows:
class Base { public String className = "Base" ;} class Derived extends Base { public String className = "Derived" ;} public class Privatematter { public static void main (string[] args) {Syste M.out.println ((Base) new Derived ()). ClassName); }}
Here, we can draw a conclusion that the hidden domain can be accessed by forcing the type conversion from the subclass. But do we associate the method's overwrite? method is not accessible through this method (unless the parent class method is called using the Super keyword in a subclass method). Because after coercion of type conversion, although the father's reference is still an instance of the subclass, the method called is also a subclass of the method. ( This is the difference between hiding and covering )
When writing a program, in addition to overwrite, pay special attention to the name of the distinction, try not to be the same.
2, Java rules: A Main method must accept a single string array parameter 3, the Java default modifier
Pay special attention to the default when it is in a relationship that is not in the same package .
Reference Blog: http://blog.csdn.net/cocojiji5/article/details/6638240
Also note that the private method within the package cannot be covered by a method declaration outside the package.
4. Final modifier action
For a method: final means that the method cannot be overwritten (for instance methods) or hidden (for static methods). For a domain, final means that the field cannot be assigned more than once.
5, cover Obscure6, shelter shadow
such as local variables and global variables
Java Puzzles-Class puzzles (ii)