Object polymorphism understanding

Object-oriented three characteristics: Packaging - Protection of property is not directly accessible to the outside; inheritance - extend the properties and functions of the class; then Polymorphisms it?
Polymorphism in Java is the most powerful place, then there is a simple but they need to take the scrutiny of the question: What is polymorphism? What is a multi-state object?
1. What is polymorphism?
, As I understand it, what is polymorphism, which literally means slightly, multiple states.
, Object-oriented multi-state properties of two withdrawals: 1, overload and override methods (some people say this is not polymorphic, however, I feel that count, because they meet the requirements of various states) ; 2 polymorphism object;
overload - depending on the number of functions performed by a method parameters and parameter types are different; override - subclass demand override the parent class method in accordance with;


1-0: Polymorphism object
transformation on 1-1 phase:
receiving sub-class instance (automatically) using the parent class object. Subclass object as a parent class object is instantiated.
Upward transition occurred, then in a subclass of those properties and methods unique to this transformation can not be the object of the saw.
define a class class A {// A
    public void fun1 () {// definition of fun1 () method
        System.out.println ( "A -> public void fun1 () {}");
    }
    public void fun2 () {
        the this .fun1 (); // call fun1 () method
    }
};
class B the extends a {
    public void fun1 () {// subclass overrides this method
        System.out.println ( "B -> public void fun1 ( ) {} ");
    }
    public void FUN3 () {// subclass redefined own unique method
        System.out.println (" B -> public void FUN3 () {} ");
    }
};
public class PolDemo01 {
    static void main public (asrgs String []) {
        B B B = new new (); // instance subclasses of objects
        A a = b; // upcast relationship
        a.fun1 (); // subclasses override this method through
        a.fun3 ();
    }
};
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
 
here a.fun3 (); this code will be given, as a the transition from b is already coming up, and it can not see a way unique to b, b to see are inherited from a method and override came and attributes.

Note the upward transition point
, however, note that the upward transition occurred, is the parent object, then call the method to those in the parent class, you will find, is still being called subclasses override off method, if there is no cover wrote these methods, the method will call the parent class, because the upward transition occurred, and said to a certain extent, he or subclass instance, in my understanding, the strip is a subclass of the unique characteristics of the subclass. (Image so you can understand, this subclass object is a bastard, a child born to non-wife had maidservant, did not give him the ability to become one of the princes of exploiting the boundary, just has accumulated more than their parents and shade power and parents are closely related, and only wife had children born before their parents have brought glory and power of the parents on the basis of continuing Kaijiangtuotu) calls the subclass unique methods and properties will be incorrect report, because he these methods properties, has been stripped, and he can not see, all the error.

{class PolDemo01 public
    public static void main (String asrgs []) {
        B B B = new new (); // instance subclasses of objects
        A a = b; // upward transition relation
        a.fun1 (); // this method subclasses override through
        a.fun2 ();
    }
};
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8


1-2 downcast:
receiving parent class object using subclass object, the parent object is changed subclass object (required casts).
define a class class A {// A
    public void fun1 () {// definition of fun1 () method
        System.out.println ( "A -> public void fun1 () {}");
    }
    public void fun2 () {
        the this .fun1 (); // call fun1 () method
    }
};
class B the extends a {
    public void fun1 () {// subclass overrides this method
        System.out.println ( "B -> public void fun1 ( ) {} ");
    }
    public void FUN3 () {
        System.out.println (" B -> public void FUN3 () {} ");
    }
};
public class PolDemo02 {
    public static void main (String asrgs [] ) {
        A a = new B (); // upward transition relation
        B b = (B) a; // downward transition occurred relationship
        b.fun1 ();
        b.fun2 ();
        b.fun3 ();
    }
};
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25


Downcast, there is a point of note:
When making the transition down, you must first make the transformation up, otherwise there will be type cast exception (ClassCastException). Because, only through the parent class, is unable to determine whether a class is a subclass, but from the subclass can extend directly through keywords, understand who its parent is directly between the two does not matter if the object the conversion will take place this exception, that is, the relationship between the object to be downcast happen, you certainly have to produce a transition upwards relationship, it is to establish a relationship between two objects such purpose.
define a class class A {// A
    public void fun1 () {// definition of fun1 () method
        System.out.println ( "A -> public void fun1 () {}");
    }
    public void fun2 () {
        the this .fun1 (); // call fun1 () method
    }
};
class B the extends a {
    public void fun1 () {// subclass overrides this method
        System.out.println ( "B -> public void fun1 ( ) {} ");
    }
    public void FUN3 () {
        System.out.println (" B -> public void FUN3 () {} ");

};
Public class PolDemo03 {
    public static void main (String asrgs []) {
        A = A new new A (); // instantiate a parent object
        B b = (B) a; // downward transition occurred relationship
        b.fun1 ();
        b.fun2 ();
        b.fun3 ();
    }
};
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25


Such as Class A, B extends A class, in the first case where the first: B b = new A () ; This message will definitely abnormal; and the second case: A a = new B () ; This is the upward transition is in order for a, B two classes of objects generated relations; after making downcast, B b = (B) a ; this would not be a problem;
2, the advantages of the object polymorphism?
Said advantage, how the most obvious, is the inevitable comparison, a thing, a good technology in the end, how do you know, compare, the saying goes, there is no comparison did not hurt, compare with before and after use in the end what difference is I know where the advantage.
2-1 problem:
design a method that can receive any subclass object class A, and the method is called in the main category.
First, do not use multi-state, this issue certainly need to use overloading to complete:

define a class class A {// A
    public void fun1 () {// definition of fun1 () method
        System.out.println ( "A -> public void fun1 () {}");
    }
    public void fun2 () {
        the this .fun1 (); // call fun1 () method
    }
};
class B the extends a {
    public void fun1 () {// subclass overrides this method
        System.out.println ( "B -> public void fun1 ( ) {} ");
    }
    public void FUN3 () {
        System.out.println (" B -> public void FUN3 () {} ");
    }
};
class C the extends A {
    public void fun1 () {// this method subclasses overwritten
        System.out.println ( "C -> public void fun1 () {}");
    }
    public void fun5(){
        System.out.println ( "C -> public void fun5 () {}");
    }
};
public class PolDemo04 {
    public static void main (String asrgs []) {
        Fun (new new B ()); // pass examples B
        fun (new C ()); // transfer of example B
    }
    public static void Fun (B B) {
        b.fun1 (); // call to override the parent class fun1 () method
    }
    public static void Fun (C C) {
        c.fun1 (); // call the parent class fun1 override () method
    }
};
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
If now the extension, A class, sub-class 1000, it is in trouble, a number of methods to increase the functionality overloaded, very bad.

Using the object polymorphic

define a class class A {// A
    public void fun1 () {// definition of fun1 () method
        System.out.println ( "A -> public void fun1 () {}");
    }
    public void fun2 () {
        the this .fun1 (); // call fun1 () method
    }
};
class B the extends a {
    public void fun1 () {// subclass overrides this method
        System.out.println ( "B -> public void fun1 ( ) {} ");
    }
    public void FUN3 () {
        System.out.println (" B -> public void FUN3 () {} ");
    }
};
class C the extends A {
    public void fun1 () {// this method subclasses overwritten
        System.out.println ( "C -> public void fun1 () {}");
    }
    public void fun5(){
        System.out.println ( "C -> public void fun5 () {}");
    }
};
public class PolDemo05 {
    public static void main (String asrgs []) {
        Fun (new new B ()); // pass examples B
        fun (new C ()); // transfer of example B
    }
    public static void Fun (a a) {
        a.fun1 (); // call to override the parent class fun1 () method
    }
};
1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
such a code, regardless of how many Class A subclass no problem. This function does not require to be modified.

Summary: 
 
polymorphism summary.

3, instanceof keyword
3-1, when to use instanceof keyword, that keyword what effect?
In fact, this question is very simple, in Java, can be determined by instanceof keyword, which is an instance of an object class.

Format: The object instanceof class name -> return content is a Boolean result.
For the previous question: a method can accept any subclass object of a class, and you can call the problem with this approach has been the perfect solution up through the transformation of polymorphic; it now has a new problem: the subclass A A's extends B, inherited the method a, while a new customized methods fun3 (), a extends C, a new customized methods fun5 (), demand is now: - when the incoming class class when the object is an instance of B, so that the object calls fun3, if the incoming object is an instance of C when it calls on fun5,

For this demand, a multi-state only by the parameters is not enough, but also in the interior by using the method instanceof keyword incoming object determination, respectively, and then performs the corresponding method.
    define a class class A {// A
        public void fun1 () {// definition of fun1 () method
            System.out.println ( "A -> public void fun1 () {}");
        }
        public void fun2 () {
            the this .fun1 (); // call fun1 () method
        }
    };
    class B the extends a {
        public void fun1 () {// subclass overrides this method
            System.out.println ( "B -> public void fun1 ( ) {} ");
        }
        public void FUN3 () {
            System.out.println (" B -> public void FUN3 () {} ");
        }
    };
    public class InstanceofDemo01 {
        static void main public (asrgs String []) {
            A = A1 new new B (); // upcast by instantiating the object   
            System.out.println ( "A a1 = new B ():" + (a1 instanceof A)) ; to true //
            System.out.println ( "A new new A1 = B ():" + (A1 the instanceof B)); // to true
            A = A2 new new A (); // upcast by instantiating the object
            System.out .println ( "A new new A2 = A ():" + (A2 the instanceof A)); to true //
            System.out.println ( "A new new A2 = A ():" + (A2 the instanceof B)); // FALSE
        }
    };
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
in development, for the downward transition, the transition must be verified in order to avoid classCastException.

define a class class A {// A
    public void fun1 () {// definition of fun1 () method
        System.out.println ( "A -> public void fun1 () {}");
    }
    public void fun2 () {
        the this .fun1 (); // call fun1 () method
    }
};
class B the extends a {
    public void fun1 () {// subclass overrides this method
        System.out.println ( "B -> public void fun1 ( ) {} ");
    }
    public void FUN3 () {
        System.out.println (" B -> public void FUN3 () {} ");
    }
};
class C the extends A {
    public void fun1 () {// this method subclasses overwritten
        System.out.println ( "C -> public void fun1 () {}");
    }
    public void fun5(){
        System.out.println("C --> public void fun5(){}") ;
    }
};
public class InstanceofDemo02{
    public static void main(String asrgs[]){
        fun(new B()) ;
        fun(new C()) ;
    }
    public static void fun(A a){
        a.fun1() ;
        if(a instanceof B){
            B b = (B) a ;
            b.fun3() ;
        }
        if(a instanceof C){
            C c = (C) a ;
            c.fun5() ;
        }
    }
};
1
2
3
4
5
6
7
8
9
10
11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
if we want to add subclass, the sure method to modify fun, so that lost the flexibility of the program, 
therefore, development, program design emphasis should be placed on the design of the parent class, only the parent class design is reasonable enough, 
the development process will be very convenient, we wanted cornerstone must be compacted, high-rise to be higher.

Development rules: 
a class never to inherit a class has achieved good, but best to go to inherit an abstract class or implement an interface.
--------------------- 
Author: Philadelphia Eagles 
Source: CSDN 
Original: https: //blog.csdn.net/jakezhang1990/article/details/68557710 
copyright notice : This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/writebook2016/article/details/84660784