Java subclass Strong-to-parent class strong Rotor class

Source: Internet
Author: User

Java inheritance

Inheritance is the child class inherits the characteristics and behavior of the parent class, so that the subclass object (instance) has the parent class's instance domain and method, or the subclass inherits the method from the parent class, so that the subclass has the same behavior as the parent class.

Java subclass Strong to Parent class

The parent class reference points to the subclass object:

The Java subclass strongly turns to the parent class, in fact still the subclass;

The reference can only invoke methods and variables defined in the parent class;

If a method in the parent class is overridden in a subclass, the method in the subclass is called when the method is called;

//A is the parent class, and B is the child classA A =NewA (); A.setname ("A1"); A.saya (); //SayAb b=NewB (); B.setname ("B1"); B.setsex (true); B.saya (); //SayA from BB.sayb ();//Sayb//objects in Java are type-promoted and remain in their original type. A a2 = (a) b;//subclass Strong to parent class, in fact is still subclassSystem.out.println (A2.tostring ());//B [Name=b1, Age=0, Sex=true]//the reference can only invoke methods and variables defined in the parent class;//A2.sayb ();//The Method Sayb () is undefined for the type A error//if a method in the parent class is overridden in a subclass , the method in the subclass is called when the method is called;A2.saya ();//SayA from B A2 is actually B, the B method is called

Java Parent class strong Rotor class

Only when the parent object itself is created with the subclass new can it be coerced into a subclass object in the future.

//atest.    A cannot is cast to atest. b A is a, not to B//  only when the parent object itself is a subclass new, it can be coerced into a subclass object in the future. // A2 is actually B and can be turned into B // B [Name=b1, Sex=true] // SayA from B // Sayb

Extended:

This is also true for list

New Arraylist<a>(); Alist.add (A); Alist.add (b);  for (A item:alist) {    + ":" + item.tostring ());     // class atest. A:A [NAME=A1]    //  class atest. b:b [Name=b1, Sex=true]}

Appendix:

 Public classA {PrivateString name;  PublicString GetName () {returnname; }         Public voidsetName (String name) { This. Name =name; }         Public voidSayA () {System.out.println ("SayA"); } @Override PublicString toString () {return"A [name=" + name + "]"; }    }         Public classBextendsA {Private Booleansex;  Public BooleanIssex () {returnsex; }         Public voidSetsex (Booleansex) {             This. Sex =sex; } @Override Public voidSayA () {System.out.println ("SayA from B"); //Super.saya ();        }         Public voidSayb () {System.out.println ("Sayb"); } @Override PublicString toString () {return"B [name=" + This. GetName () + ", sex=" + Sex + "]"; }    }     Public Static voidMain (string[] args) {//A is the parent class, and B is the child classA A =NewA (); A.setname ("A1"); A.saya (); //SayAb b=NewB (); B.setname ("B1"); B.setsex (true); B.saya (); //SayA from BB.sayb ();//Sayb//objects in Java are type-promoted and remain in their original type. A a2 = (a) b;//subclass Strong to parent class, in fact is still subclassSystem.out.println (A2.tostring ());//B [Name=b1, Sex=true]//the reference can only invoke methods and variables defined in the parent class;//A2.sayb ();//The Method Sayb () is undefined for the type A error//if a method in the parent class is overridden in a subclass , the method in the subclass is called when the method is called;A2.saya ();//SayA from B A2 is actually B, the B method is called//b b2 = (b) A;//atest. A cannot is cast to atest. b A is a, I can't go to B.//only when the parent object itself is created with the subclass new can it be coerced into a subclass object in the future.b b2 = (b) A2;//A2 is actually B and can be turned into BSystem.out.println (B2.tostring ());//B [Name=b1, Sex=true]B2.saya ();//SayA from BB2.sayb ();//SaybList<A> alist =NewArraylist<a>();        Alist.add (a);        Alist.add (b);  for(A item:alist) {System.out.println (Item.getclass ()+ ":" +item.tostring ()); //class atest. A:A [NAME=A1]//class atest. b:b [Name=b1, Sex=true]        }    }

Java subclass Strong-to-parent class strong Rotor class

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.