12-15java of object-oriented polymorphism operations

Source: Internet
Author: User

1. Polymorphism 1. Concept

Transition up (auto-complete)

Subclass into Parent class

Parent class Parent Class object = Subclass Instantiation Object

Downward transition (mandatory)

The parent class is transformed into a subclass and must first be transformed

Subclass Subclass Object = (subclass) Parent Class instantiation Object

2.sub-class objects are transformed upward
This program is a polymorphism test class a//definition class a{public void fun1 ()//define Class A Fun1 method {System.out.println ("This is the Fun1 method of Class A");} public void Fun2 () {this.fun1 ();}} Class B extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class B override");} public void Fun3 () {System.out.println ("This is the Fun3 method for Class B");}} public class  testpol{public static void Main (string[] args) {b b = new B ();//define a subclass of instantiated object A = b;//subclass object to be transformed upward a.fun1 ();//This Party The quilt class is overwritten, and the output is a.fun2 ();//This method covers the quilt class, the output is overwritten}}

Description: The use of the upward transformation method is a subclass-covered method.

Note: only two methods can be seen in A , three methods are seen in B , after transformation, new methods defined in the subclass cannot be found because the parent class is manipulated.

This program is a polymorphism test class a//definition class a{public void fun1 ()//define Class A Fun1 method {System.out.println ("This is the Fun1 method of Class A");} public void Fun2 () {this.fun1 ();}} Class B extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class B override");} public void Fun3 () {System.out.println ("This is the Fun3 method for Class B");}} public class  testpol{public static void Main (string[] args) {b b = new B ();//define a subclass of instantiated object A = b;//subclass object to be transformed upward a.fun1 ();//This Party The quilt class is overwritten, and the output is a.fun3 ();//New Method}}
Error message Tip:

TESTPOL.JAVA:31: error : symbol not found

A.fun3 (); New Method

^

Symbol : method fun3 ()

Location : variable a of type a

3. Parent-Class object down transformation
This program is a polymorphism test class a//definition class a{public void fun1 ()//define Class A Fun1 method {System.out.println ("This is the Fun1 method of Class A");} public void Fun2 () {this.fun1 ();}} Class B extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class B override");} public void Fun3 () {System.out.println ("This is the Fun3 method for Class B");}} public class  testpol{public static void Main (string[] args) {b b = new B ();//define subclass of instantiated Object A = b;//Subclass object up B C = (b) a;/ /parent class down Transformation c.fun1 ();//This method covers the quilt class, and the output is the overwritten c.fun2 ();//New Method}}

There are three methods in class B , all of which can be called.

Note: When you perform a down-conversion operation,

This program is a polymorphism test class a//definition class a{public void fun1 ()//define Class A Fun1 method {System.out.println ("This is the Fun1 method of Class A");} public void Fun2 () {this.fun1 ();}} Class B extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class B override");} public void Fun3 () {System.out.println ("This is the Fun3 method for Class B");}}  public class  testpol{public static void Main (string[] args) {//b b = new B ();//The instantiation object that defines the subclass//a A = b;//sub-class object is transformed upward a = new A ();//define the instantiated object of the parent class B C = (b) a;//the parent class down Transformation c.fun1 ();//This method covers the quilt class, the output is the overwritten c.fun2 ();//New Method}}
If you do not go up first, there will be an error message.

Exception in thread "main" java.lang.classcastexception:a cannot is cast to B

At Testpol.main (testpol.java:31)

4. Application of Object polymorphism

Requires designing a method that can invoke an object of any subclass of Class A.

This program is a polymorphism test class a//definition class a{public void fun1 ()//define Class A Fun1 method {System.out.println ("This is the Fun1 method of Class A");} public void Fun2 () {this.fun1 ();}} Class B extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class B override");} public void Fun3 () {System.out.println ("This is the Fun3 method for Class B");}} Class C extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class C override");} public void Fun4 () {System.out.println ("This is the Fun4 method for Class C");}} public class  testpol{public static void Main (string[] args) {Fun (new B ())); Fun (new C ());} public static void Fun (b b) {b.fun1 ();} public static void Fun (c c) {c.fun1 ();}}

If there are too many subclasses to accomplish this goal, you can use the object polymorphic line to complete.

This program is a polymorphism test class a//definition class a{public void fun1 ()//define Class A Fun1 method {System.out.println ("This is the Fun1 method of Class A");} public void Fun2 () {this.fun1 ();}} Class B extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class B override");} public void Fun3 () {System.out.println ("This is the Fun3 method for Class B");}} Class C extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class C override");} public void Fun4 () {System.out.println ("This is the Fun4 method for Class C");}} public class  testpol{public static void Main (string[] args) {Fun (new B ())); Fun (new C ());} public static void Fun (a a) {a.fun1 ();}}
2.instanceofKey Words

Frequently used in object transformation

1. Concept

use instanceof in Java to determine which class an object is actually.

Object instanceof class ----------------> He returned a boolean type .

This procedure is isntanceof test class a//definition class a{public void fun1 ()//define Class A Fun1 method {System.out.println ("This is the Fun1 method of Class A");} public void Fun2 () {this.fun1 ();}} Class B extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class B override");} public void Fun3 () {System.out.println ("This is the Fun3 method for Class B");}} public class Testinstanceof {public static void main (string[] args) {b b = new B ();//define a subclass of instantiated object A = b;//up transformation A a2 = new A ( ); System.out.println (a instanceof a); System.out.println (a instanceof B); SYSTEM.OUT.PRINTLN (A2 instanceof A); SYSTEM.OUT.PRINTLN (A2 instanceof B);}
1. Role

If an instance of class B is passed in , call fun3, if it is a class C call fun4

This procedure is isntanceof test class a//definition class a{public void fun1 ()//define Class A Fun1 method {System.out.println ("This is the Fun1 method of Class A");} public void Fun2 () {this.fun1 ();}} Class B extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class B override");} public void Fun3 () {System.out.println ("This is the Fun3 method for Class B");}} Class C extends a//Class B inherits class A{public void Fun1 ()//Overwrite fun1 method {System.out.println ("This is the Fun1 method of Class C override");} public void Fun4 () {System.out.println ("This is the Fun4 method for Class C");}} public class Testinstanceof {public static void main (string[] args) {Fun (new B ()); Fun (new C ());} public static void Fun (a a) {if (a instanceof B) {A a1 = a;//now A is a subclass, first the conversion of b b = (b) a1;//the parent class down B.fun3 ();} if (a instanceof C) {A a1 = a;//now A is a subclass, the first is to convert c C = (c) a1;//parent class to convert down C.fun4 ();}}

Note: In development, the down-transition operation is validated to ensure that there are no problems.

If you want to add a new subclass, modify the fun method so that the program loses its flexibility. So the programming focus is on the parent class. As long as reasonable enough, development is very convenient.

A class should never inherit a class that has already been implemented, inheriting only abstract classes or implementing interfaces.


I wish you all a healthy and happy.








12-15java of object-oriented polymorphism operations

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.