Java static property, static method, non-static property, non-static method inheritance problem simple understanding

Source: Internet
Author: User

Package com.book.web.controller;/** * Parent class * @author Liweihan * */public class Parent {public String normalstr = "Parent class non-static property";p Ublic static String staticstr = "Parent class static Property";p ublic string changestr = "Parent class-variable";p ublic static string changestaticstr = "Parent class-variable-static State ";p ublic void Normalmethod () {System.out.println (" parent class non-static Method! ");} public static void Staticmethod () {System.out.println ("Parent class static Method! ");} public void Changemethod () {changestr = "Change the non-static property of the parent class"; changestaticstr = "Change the static property of the parent class";}}
package com.book.web.controller;/** *  subclass Inherits Parent class  *  @author  liweihan * */public class child extends parent{public string  normalStr =  "Subclass-Non-static attribute";p ublic static string staticstr =  "sub-class-static property";//@ Overridepublic void normalmethod ()  {system.out.println ("Subclass-Non-static Method!");} /** *  static methods cannot be overridden, so remove override *///@Overridepublic  static void staticmethod ()  {system.out.println ("Subclass-static Method! ");}}
package com.book.web.controller;/** *  Test class  *  @author  liweihan * */ Public class test {public static void main (String[] args)  {Child  Child = new child (); System.out.println (CHILD.NORMALSTR); System.out.println (CHILD.STATICSTR); Child.normalmethod (); Child.staticmethod (); System.out.println ("-------------------  static properties, static methods and non-static properties, non-static methods can be   inherited");//The parent class points to such instances Parent child1  = new child (); System.out.println (CHILD1.NORMALSTR); System.out.println (CHILD1.STATICSTR); Child1.normalmethod (); Child1.staticmethod (); System.out.println (" ==================  static properties, static methods, and non-static properties can all be   inherited   and   hidden    Not be able to be rewritten ");//Gets the value of the variable parent parent = new parent (); System.out.println ("1 ... "  + parent.changestr); System.out.println ("1 --- "  + parent.changestaticstr); Child child2 = new child (); System.out.printlN ("2 ...  sub-class gets the parent non-static property value: "  + child2.changestr); System.out.println ("2 ---  subclass gets the parent class static property value: "  + child2.changestaticstr); Parent.changemethod (); System.out.println ("3 ... " gets the non-static property value &NBSP after the parent class has changed;: " + child2.changestr); System.out.println ("4 ...  sub-class gets the static property value of the parent class change:"  + child2.changestaticstr); System.out.println (" -------------  static property values in the parent class, after the parent class is initialized, the subclass can be used directly!") However, non-static properties, it is not possible! ;/** *  conclusion:  One, for non-static properties and methods: <1.> for non-static properties, subclasses can inherit non-static properties of the parent class. However, when the subclass and the parent class have the same non-static property,,    does not override and overwrite the non-static property of the parent class, only the non-static property of the parent class is hidden.  <2.> for non-static methods, subclasses can inherit non-static methods of the parent class and can override non-static property methods that override the parent class.    second, for static properties and methods  <1.> for static properties, subclasses can inherit the static properties of the parent class. However, as with non-static properties, they are hidden.  <2.> for static methods, subclasses can inherit static methods from the parent class. However, the subclass cannot override the static method of the parent class,      the static method of the parent class is hidden by the same name as the child class.   Http://www.cnblogs.com/kabi/p/5181941.html <1.>java static properties and static methods can be inherited, but not rewritten (overwrite) but hidden.  <2.> static methods and properties belong to the class, and are called directly through the class name. Method name, do not need to inherit the machineSystem can be called.   If static methods and properties are defined in a subclass, then the static methods   or properties of the parent class are called "hidden",  if you want to invoke the static methods and properties of the parent class, simply pass the parent class name. method name or variable name complete,  as to whether inheritance one says, Subclasses are inherited static methods and properties, but   are not the same as instance methods and properties, and there is a "hidden" case.   <3.> static Properties, static methods, and non-static properties can be   inherited   and   hidden    and cannot be overridden,                non-static methods can be inherited and rewritten,  */}}


This article is from the "My Java World" blog, so be sure to keep this source http://hanchaohan.blog.51cto.com/2996417/1948919

Java static property, static method, non-static property, non-static method inheritance problem simple understanding

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.