parent Class (parental): Public classParent { PublicString name = "Parent Parental class Property"; Public voidsay () {System.out.println ("Parent say is called" + This. GetClass ()); }} subclass (Child): Public classChildextendsParent {/*** Attributes can only be hidden and cannot be overwritten*/ PublicString name = "Child sub-class attribute"; Public Static voidMain (string[] args) {child C=NewChild ();//Normal ObjectC.say (); Parent P=NewChild ();//Upward TransformationP.say (); /*** When subclass child and parent have the name attribute: * c.name = Child Subclass Property * P.name = Parent Class Property * When Subclass child Without the Name property, when the parent class has the property: * c.name = Parent property of the parents * P.name = Parent Property*/System.out.println ("C.name =" + c.name);//c.name = ChildSystem.out.println ("p.name =" + p.name);//P.name = parent } /*** Overriding parent class methods*/ Public voidSay () {//Subclass Method OneSystem.out.println ("Child say is called" + This. GetClass ()); } /*** Overloading--subclass Method One * When the subclass method does not exist, the subclass Method II looks like the programmer intentionally overwrites the function of say (), but the type definition of the method is slightly imprecise. * However, the compiler now assumes that the programmer is intentionally "overloaded" rather than "overwritten." */ Public voidSayints) {//Subclass Method TwoSystem.out.println ("Child say is called" + This. GetClass ()); } /*** * method signature consists of the method name and a list of parameters (the order and type of the parameters of the method), which is called overloading when defining two or more methods with the same name. * The parameter list for the same name member is not the same when overloaded (parameter order and type). * The following say2 three methods are the embodiment of overloading*/ Public voidSay2 () {//Subclass Method ThreeSystem.out.println ("Child say2 is called"); } Public voidSay2 (intA) {//Subclass Method FourSystem.out.println ("Child say2 is called"); } Public intSay2 (String a) {//Subclass Method FiveSystem.out.println ("Child say2 is called"); return0; }}
java--Overloading and rewriting