Java Puzzles-Class puzzles (ii)

Source: Internet
Author: User

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)

Related Article

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.