1 Public classtestpolymorphic {2 3 Public Static voidMain (string[] args) {4 5 /*the characteristics of object-oriented polymorphism are divided into two kinds*/6 7 /*The first is a reference polymorphism, which means that the parent class can point to a reference to this class, or to a child class reference*/8Parent x =NewParent ();//point to this class reference9Parent s =NewChild ();Ten One /*But the subclass cannot point to the parent class reference*/ A //Child C = new Parent (); //This will be an error . - - /*The 22nd is the method polymorphism, when creating this kind of object, called the method of the class, when creating the subclass object is called the subclass overrides or inherits the method*/ theX.test ();//The method in the parent class executes!!! -S.test ();//The method in the subclass executes!!! - S.fn (); - } + - } + A classParent { at Private intAge ; - PrivateString name; - Public intGetage () { - returnAge ; - } - Public voidSetage (intAge ) { in This. Age =Age ; - } to PublicString GetName () { + returnname; - } the Public voidsetName (String name) { * This. Name =name; $ }Panax Notoginseng - Public voidTest () { theSystem.out.println ("The method in the parent class has executed!!! "); + } A the Public voidfn () { +System.out.println ("Subclass does not have this method, but it also executes because of inheritance.")); - } $ } $ - classChildextendsParent { - Public voidTest () { theSystem.out.println ("The method in the subclass executes!!!") "); - } Wuyi}
Java Features---polymorphism