The use of Java object polymorphism __java

Source: Internet
Author: User

Reproduced from: http://blog.csdn.net/aotumemedazhao1996/article/details/52818762

Java interfaces can be instantiated.


interface cannot be instantiated. However, an interface object can point to its implementation class object.
Interfaces can be considered as special abstract classes, except that all methods are abstract (not implemented), and the interface methods are default public abstract.
So it can't be instantiated.
Like what:

The List Set map is an interface
Implemented first when used

list<string> list = new arraylist<> ();

A bit like the feeling of a pointer. The factory interface can be used to represent the class that implements it. Like what:
public interface thing;
Public class fruit implements thing;
Thing something = new fruit ();
This something refers to fruit.

Interface programming is to implement multiple inheritance.

Object polymorphism is one of the most important concepts in object-oriented, and there are two main manifestations in Java:

1. Overloading and overriding of methods
2. The polymorphism of the object
For overloaded and overridden methods, the following focuses on the polymorphism of the object.
The polymorphism of the object is mainly divided into the following two types:
(1) Upward Transformation: Subclass Object-"Parent object";
(2) Downward transformation: The Parent Object-"Subclass Object";
For the upward transition, the program will automatically complete, and for downward transition, must clearly indicate the subclass type to transition, the format is as follows:

Object transition Upward: Parent class object = Subclass instance;
Object downward transition: Subclass Subclass Object = (subclass) Parent object;
1 2

The following actions are made to move up the object:
"Object Transitions Up"

Class A
{public
    void fun1 () {
        System.out.println ("A-->public void Fun1 ()");
    }
    public void Fun2 () {
        this.fun1 ();
    }
}
Class B extends a
{
    //overwrite method in a (a) public
    void Fun1 () {
        System.out.println ("B-->public Void Fun () 1");
    }
    The method that is defined by itself in subclasses is public
    void Fun3 () {
        System.out.println ("B-->oublic void Fun3 ()");
    }
public class PolDemo01
{public
    static void Main (string[] args) {
        b b=new b ();//define Subclass instantiation Object
        A a=b;// There has been an upward transition in the relationship, subclasses----"Parent class
        a.fun1 ();//This method quilt class overwrite
    }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27-28

Program Run Result:

b-->public void Fun () 1
1

The above program is an object of upward transformation of the relationship, from the results of the operation of the program can be found, although the use of the parent class object to invoke the Fun1 () method, but actually called the method is a quilt-type overridden method, that is, if the object occurred after the upward transition relationship, The method that is called must be a quilt-type-overridden method. However, it is important to note at this point that object A is not able to invoke the Fun3 () method in Class B. Because this method is defined only in subclasses and not in the parent class, if you want to invoke the method that you defined in the subclass, you must use the subclass instance, so you can transition the object down.
"Object Downward Transition"

Class A
{public
    void fun1 () {
        System.out.println ("A-->public void Fun1 ()");
    }
    public void Fun2 () {
        this.fun1 ();
    }
}
Class B extends a
{
    //overwrite method in a (a) public
    void Fun1 () {
        System.out.println ("B-->public Void Fun () 1");
    }
    The method that is defined by itself in subclasses is public
    void Fun3 () {
        System.out.println ("B-->oublic void Fun3 ()");
    }
public class PolDemo01
{public
    static void Main (string[] args) {
        A a=new B ();//There has been an upward transition relationship, subclass--"Parent class
        B" b= (B) a;//a downward transition relationship
        b.fun1 ();//Invoke Method-overridden method
        b.fun2 ();//calling method of parent class
        b.fun3 ();//calling method of subclass definition
    }
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Run Result:

b-->public void Fun () 1
b-->public void Fun () 1
b-->oublic void Fun3 ()
1 2 3

As you can see from the above program, if you want to call the subclass's own method, you must only instantiate an object with a subclass, in addition, call the Fun2 () method inherited from the parent class in the subclass object, and the Fun2 () method calls the Fun1 () method, because the Fun1 () method is already overridden by the quilt class. So the method called at this time is a quilt-like method.

Periodic summary:

1. The main features of the upward transition, subclasses for the parent object instantiation, the parent class to accept the function of the subclass object, the method of invocation must be a subclass of the method overridden; ( upward transformation to find common )

2. The main characteristics of the downward transition, subclasses extend some features, these functional parent classes are not defined, you need to perform a downward transition to invoke the characteristics of the subclass-specific features;
Object downward transition Requirements:
Before the object is transformed, the object must be transformed upward, otherwise the object transformation exception will occur.
Once you understand the polymorphism of an object, what is the purpose of this concept? The following requires the design of a method that accepts any subclass object of Class A and invokes the method, which, if not the polymorphism of the object, will certainly use the following form of code:
"Do not use object polymorphism to implement functionality"

Class A {public void fun1 () {System.out.println ("a-->public void Fun1 ()");
    public void Fun2 () {this.fun1 ();
    Class B extends a {//overwrite method public void Fun1 () {System.out.println ("b-->public void Fun1 ()") in a
    The method defined in the//subclass is public void Fun3 () {System.out.println ("b-->poublic void Fun3 ()");
    Class C extends A {public void fun1 () {System.out.println ("c-->public void Fun1 ()");
    public void Fun5 () {System.out.println ("c-->public void Fun3 ()"); } public class PolDemo04 {public static void main (string[] args) {Fun (new B ());//pass Instance of Class B fun (new C ()
    )//Transitive C class instance} public static void fun (b b) {//Receive B class instance b.fun1 ();
    public static void Fun (c c) {//Receive instance C.FUN1 () of Class C; }
}
1 2 3 4 5 6 7 8 9 10 11 12 13-14
Related Article

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.