12-15java object-oriented Polymorphism

Source: Internet
Author: User

12-15java object-oriented Polymorphism
1. polymorphism 1. Concept

Upward Transformation (automatically completed)

Child class to parent class

Parent class Object = subclass instantiated object

Downward Transformation (mandatory)

The parent class must first be transformed to a subclass.

Subclass subclass object = (subclass) parent class instantiation object

2. The subclass object is transformed upwards.
// This program is A test of polymorphism class A // defines class A {public void fun1 () // defines the fun1 method of class A {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 () // override fun1 method {System. out. println (this is the fun1 method overwritten by Class B);} public void fun3 () {System. out. println (this is the fun3 method of class B);} public class TestPol {public static void main (String [] args) {B B B = new B (); // define the instantiated object A = B of the subclass. // The subclass object is transformed up to. fun1 (); // This method is overwritten by the quilt class, and the output is. fun2 (); // This method is overwritten by the quilt class, and the output is overwritten }}

Note: The method for upward transformation is a subclass override method.

Note: Only two methods can be seen in A, and three methods can be seen in B. After the transformation, the new method defined in the subclass cannot be found because the parent class is operated.

// This program is A test of polymorphism class A // defines class A {public void fun1 () // defines the fun1 method of class A {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 () // override fun1 method {System. out. println (this is the fun1 method overwritten by Class B);} public void fun3 () {System. out. println (this is the fun3 method of class B);} public class TestPol {public static void main (String [] args) {B B B = new B (); // define the instantiated object A = B of the subclass. // The subclass object is transformed up to. fun1 (); // This method is overwritten by the quilt class, and the output is. fun3 (); // New Method }}
Error message:

TestPol. java: 31: Error: cannot find the symbol

A. fun3 (); // New Method

^

Symbol: Method fun3 ()

Location: variable A of type

3. Parent Class Object downward Transformation
// This program is A test of polymorphism class A // defines class A {public void fun1 () // defines the fun1 method of class A {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 () // override fun1 method {System. out. println (this is the fun1 method overwritten by Class B);} public void fun3 () {System. out. println (this is the fun3 method of class B);} public class TestPol {public static void main (String [] args) {B B B = new B (); // define the instantiation object of subclass A a = B; // The subclass object is transformed upwards to B c = (B) a; // The parent class is transformed downward to c. fun1 (); // This method is overwritten by the quilt class, and the output is overwritten by c. fun2 (); // New Method }}

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

Note: During the downward transformation operation,

// This program is A test of polymorphism class A // defines class A {public void fun1 () // defines the fun1 method of class A {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 () // override fun1 method {System. out. println (this is the fun1 method overwritten by Class B);} public void fun3 () {System. out. println (this is the fun3 method of class B);} public class TestPol {public static void main (String [] args) {// B B B = new B (); // define the instantiated object of the subclass // A a = B; // The subclass object is transformed upwards into A = new (); // define the instantiated object B c = (B) a of the parent class; // The parent class is transformed down to c. fun1 (); // This method is overwritten by the quilt class, and the output is overwritten by c. fun2 (); // New Method }}
If the preceding operations are not performed, an error message is displayed.

Exception in thread main java. lang. ClassCastException: A cannot be cast to B

At TestPol. main (TestPol. java: 31)

4. Application of object Polymorphism

A method is required to call objects of any subclass of Class.

 

// This program is A test of polymorphism class A // defines class A {public void fun1 () // defines the fun1 method of class A {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 () // override fun1 method {System. out. println (this is the fun1 method overwritten by Class B);} public void fun3 () {System. out. println (this is the fun3 method of class B);} class C extends A // class B inherits class A {public void fun1 () // override fun1 method {System. out. println (this is the class C override fun1 method);} public void fun4 () {System. out. println (this is the fun4 method of 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 sub-classes to accomplish this goal, you can use the object multi-state row.

// This program is A test of polymorphism class A // defines class A {public void fun1 () // defines the fun1 method of class A {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 () // override fun1 method {System. out. println (this is the fun1 method overwritten by Class B);} public void fun3 () {System. out. println (this is the fun3 method of class B);} class C extends A // class B inherits class A {public void fun1 () // override fun1 method {System. out. println (this is the class C override fun1 method);} public void fun4 () {System. out. println (this is the fun4 method of class C);} public class TestPol {public static void main (String [] args) {fun (new B ()); fun (new C ();} public static void fun (A) {. fun1 ();}}
2. instanceof keyword

Often used in object Transformation

1. Concept

In java, instanceof is used to determine the actual class of an object.

Object instanceof class ----------------> the returned value belongs to the boolean type.

// This program is the isntanceof test class A // defines class A {public void fun1 () // defines the fun1 method of class A {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 () // override fun1 method {System. out. println (this is the fun1 method overwritten by Class B);} public void fun3 () {System. out. println (this is the fun3 method of class B);} public class TestInstanceOf {public static void main (String [] args) {B B B = new B (); // define the instantiation object of the subclass A a = B; // converts 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 the input is a Class B instance, call fun3. If the input is Class C, call fun4.

// This program is the isntanceof test class A // defines class A {public void fun1 () // defines the fun1 method of class A {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 () // override fun1 method {System. out. println (this is the fun1 method overwritten by Class B);} public void fun3 () {System. out. println (this is the fun3 method of class B);} class C extends A // class B inherits class A {public void fun1 () // override fun1 method {System. out. println (this is the class C override fun1 method);} public void fun4 () {System. out. println (this is the fun4 method of 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, convert B B = (B) a1; // The parent class to B. fun3 ();} if (a instanceof C) {A a1 = a; // now a is a subclass. First, convert C c = (C) a1 to C = (c; // The parent class is converted down to c. fun4 ();}}}

Note: during development, the downward transformation operation should be verified to ensure that no problems occur.

If you want to add a new subclass, You need to modify the fun method so that the program loses flexibility. Therefore, the focus of program design is on the parent class. As long as it is reasonable enough, development is very convenient.

A class never inherits classes that have been implemented. It can only inherit abstract classes or interfaces.

 







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.