Object-oriented (advanced) -- Object Polymorphism

Source: Internet
Author: User

Object-oriented (advanced) -- Object Polymorphism

Master the use of upward transformation of objects for downward Transformation
Understanding the limitations of object Transformation

Object polymorphism is an important property of object orientation.
The orientation mainly includes the following two manifestations:
Method overloading and overwriting
Object Polymorphism

There are two types of object polymorphism:
Upward transition: subclass Object> parent class Object
The upward transformation process is automatically completed. format:
Object upward transition: parent class Object = subclass instance

Downward transition: parent class Object> subclass object
The type of the subclass to be converted must be specified in the following format:
Object downward transition: subclass object = (subclass) parent class instance

Example of object upward Transformation:
Class {
Public void fun1 (){
System. Out. println ("A --> Public void fun1 (){}");
}
Public void fun2 (){
This. fun1 ();
}
};
Class B extends {
Public void fun1 (){
System. Out. println ("B --> Public void fun1 (){}");
}
Public void fun3 (){
System. Out. println ("c --> Public void fun3 (){}");
}
};
Public class poldemo01 {
Public static void main (string ARGs []) {
B = new B (); // defines subclass instantiation
A A = B; // The relationship between the above-mentioned Transformations
A. fun1 (); // This method has been overwritten by the quilt class
}
}

Example of object downward Transformation

Class {
Public void fun1 (){
System. Out. println ("A --> Public void fun1 (){}");
}
Public void fun2 (){
This. fun1 ();
}
};
Class B extends {
Public void fun1 (){
System. Out. println ("B --> Public void fun1 (){}");
}
Public void fun3 (){
System. Out. println ("c --> Public void fun3 (){}");
}
};
Public class poldemo02 {
Public static void main (string ARGs []) {
A A = new B (); // upward Transformation
B = (B) A; // downward Transformation
B. fun1 ();
B. fun2 ();
B. fun3 ();
}
}

Note: Requirements for object downward Transformation
Before you perform the downward transformation of an object, you must first perform the upward transformation of the object. Otherwise, an object conversion exception occurs.

Incorrect transformation example:

Class {
Public void fun1 (){
System. Out. println ("A --> Public void fun1 (){}");
}
Public void fun2 (){
This. fun1 ();
}
};
Class B extends {
Public void fun1 (){
System. Out. println ("B --> Public void fun1 (){}");
}
Public void fun3 (){
System. Out. println ("c --> Public void fun3 (){}");
}
};
Public class poldemo03 {
Public static void main (string ARGs []) {
A A = new A (); // upward Transformation
B = (B) A; // downward Transformation
B. fun1 ();
B. fun2 ();
B. fun3 ();
}
}

Error code:
Exception in thread "Main" Java. Lang. classcastexception: A cannot be cast to B
At poldemo03.main (poldemo03.java: 20)

The role of object polymorphism:
The following requirements exist:
Design a method that can interface any subclass object of Class A and call the method.

Class {
Public void fun1 (){
System. Out. println ("A --> Public void fun1 (){}");
}
Public void fun2 (){
This. fun1 ();
}
};
Class B extends {
Public void fun1 (){
System. Out. println ("B --> Public void fun1 (){}");
}
Public void fun3 (){
System. Out. println ("B --> Public void fun3 (){}");
}
};
Class C extends {
Public void fun1 (){
System. Out. println ("c --> Public void fun1 (){}");
}
Public void fun4 (){
System. Out. println ("c --> Public void fun3 (){}");
}
};
Public class poldemo04 {
Public static void main (string ARGs []) {
Fun (New B ());
Fun (New C ());
}
Public static void fun (){
A. fun1 ();
}
}

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.