Properties of Java doubts hidden

Source: Internet
Author: User

<span style= "FONT-SIZE:18PX;" >import java.util.*;p ublic class sixtysixth {        public static void main ( String[] args) {                SYSTEM.OUT.PRINTLN (New Child (). Name);                 System.out.println (New Child (). Count);                 System.out.println (New Child (). Parent);                 System.out.println ((Base) New Child ()). Count);                 System.out.println ((Base) New Child ()). Name);               // System.out.println ((Base) New Child ()). Uniq);                 Child.fun ();//Call Subclass hidden methods                  new Child (). F1 ()//Call the subclass inherited method, call the overridden F () in F1 ()         }}class Child extends base{        public String Name =  "Child";//mask non-static name        public static int count=100;//of the parent class The static count        public static int uniq = 200;//Subclass unique static     is masked by the parent class      public static void Fun () {                 System.out.println ("Fun:child");       }         public void f () {                 System.out.println ("override");       } }clbase{        Public String name =  "Base";         public static int count = 0;        public static int parent=5;//parent class unique Static & nbsp;       public int BC = 6;//non-static BC unique to parent class, can be inherited by quilt class          Public Base () {                count++;       }        public void F1 () {                 f ();        }        public static void Fun () {                 System.out.println ("Fun:base");                //f ();       }        public void f () {                 System.out.println (" Original ");       }}</span>

Child
100
5
4
Base
Fun:child
Override

About inheritance

Static methods, general methods and static properties, the general properties will be the Quilt class inheritance, but only the general method can be overridden, polymorphic, the other three are inherited or hidden.

If it is hidden, then access is the attribute in the subclass, whether it is reasonable or not.

Such as:

Import java.util.*;p Ublic class Sixtysixth {public        static void Main (string[] args) {                System.out.println (new C () . a);}        } Class C extends b{        private int a = 0;} Class b{public        int a = 1;}

Who is the person who is visiting a? Is it output 0 or 1?

Sixtysixth.java:12: Error: A can access privateSystem.out.println (new C (). a) in C;                          ^1 a mistake.
Neither output 0 nor output 1, but an error. That is, after the hidden property, only in the class with super access to the hidden properties or methods.

Import java.util.*;p Ublic class Sixtysixth {public        static void Main (string[] args) {                new C (). f ();        }} Class C extends b{        private int a = 0;        public void F () {                System.out.println ("My A=" +a);                System.out.println ("A= of My Father's class" +super.a);}        } Class b{public        int a = 1;}

My a=0, the a=1 of my father's class.


Properties of Java doubts hidden

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.