Up and down styling in Java

Source: Internet
Author: User

Package com.study.oop.day01;/** * If a method is static, its behavior is not polymorphic * @author Luhonggang * @date June 5, 2017 * @time pm 4:19:21 * @sinc E 1.0 * Constructors are not polymorphic, they are actually static methods, * except that the static declaration is implicit. Therefore, the constructor cannot be override. */public class Staticdemo {public static void Main (string[] args) {Staticsuper ss = new Staticsub ();Ss.                              Staticmethod (); The static method in the parent class is called a subclass that cannot both override the static method in the parent classSs. Notstaticmethod ();Ss.            Thismethodisnotexistsinsuper (); Program compilation does not pass, the object after initialization can only call methods and properties of the parent class type/*** Styling up in Java: References to parent class types to objects of subclasses* Staticsuper SS = new Staticsub ();*/Staticsuper SS2 = new Staticsuper (); Down stylingStaticsub SS3 = (staticsub) SS;SS2. Thismethodisnotexistsinsuper ();Staticsub SS4 = (staticsub) new Staticsuper ();//compile Pass, run exceptionSs3. Thismethodisnotexistsinsuper (); Run-time exceptionSystem.out.println (Ss3.name);So when you look down, you need to use instanceof.if (SS2 instanceof staticsub) {//downward styling when forced to be judged falseStaticsub ss5 = (staticsub) ss2; System.out.println (Ss5.name);}else{System.out.println ("The object on the left is not an instance of the right class");}if (ss instanceof Staticsub) {//TrueStaticsub ss6 = (staticsub) SS;System.out.println ("The object on the left is an instance of the right class");SS6. Thismethodisnotexistsinsuper ();}}}class staticsuper{public static void Staticmethod () {System.out.println ("I am the static method of the parent class");}public void Notstaticmethod () {System.out.println ("I am a non-static method of the parent class");}}class Staticsub extends staticsuper{String name = "10010";public static void Staticmethod () {System.out.println ("I am a static method of a subclass");}public void Notstaticmethod () {System.out.println ("I am a non-static method of a subclass");}/*** This method does not exist in the parent class and is unique in subclasses only**/public void Thismethodisnotexistsinsuper () {System.out.println ("Just a subclass-unique method, which is an extension of this class");}}

Up and down styling 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.