Java parent class and subclass Polymorphism

Source: Internet
Author: User
Parent class: public class base {Public String S1 = "base string"; public static string S2 = "Base Static string"; Public void show () {system. out. println (this. s1);} public void show_static () {system. out. println (S2 );}}

Subclass:

public class child extends base {public String s1 = "child string";public static String s2 = "child static string";public void show(){System.out.println(this.s1);}public void show_static(){System.out.println(s2);}}

Call

public class mytest {public static void main(String[] args){/*base b1 = new base();b1.show();child c1 = new child();c1.show(); */base t1 = new child();t1.show();t1.show_static();System.out.println(t1.s1);System.out.println(t1.s2);}}

Result:

child stringchild static stringbase stringbase static string

Conclusion:

Polymorphism supports the instance method. For instance fields and static methods of instances, polymorphism is not supported.

That is, the reference of the instance method corresponds to the instance, while the static content reference corresponds to the variable.

-------------------------------------------------------- Dividing line of the subclass instance called by the parent class variable -----------------------------------------------------------------------

Generally, we can see in the code that the parent class variables are used to receive subclass instances, and then call the corresponding method. There is no special usage in this way, which reduces the coupling of code, makes the code easy to maintain.

Let's first look at an example,

public class base {public String s1 = "base string";public static String s2 = "base static string";public void show(){System.out.println(this.s1);}public void show_static(){System.out.println(s2);}}public class child extends base {public String s1 = "child string";public static String s2 = "child static string";public void show(){System.out.println(this.s1);}public void show_static(){System.out.println(s2);}}public class child2 extends base {public String s1 = "child2 string";public static String s2 = "child2 static string";public void show(){System.out.println(this.s1);}public void show_static(){System.out.println(s2);}}

Call class:

public class mytest {public void f1(child cc){cc.show();}public void f2(base bb){bb.show();}public static void main(String[] args){mytest m1 = new mytest();child c1 = new child();m1.f1(c1);base b1 = new child();m1.f2(b1);}}

There are two methods above, F1, F2 started to do the same thing; then there is a need to change, the Child class should be changed to the child2 class; for F1, we need to change three places [declare instance 2 and parameter Declaration 1], while for F2, we only need to change one place [instance ].

Think about it. If the parameter declaration is not one but multiple, or even different classes, there will be more places to change in different packages, and it is easy to leak. Therefore, one F1 process is not well maintained by one F2 process.

After changing to child2, see:

public class mytest {public void f1(child2 cc){cc.show();}public void f2(base bb){bb.show();}public static void main(String[] args){mytest m1 = new mytest();child2 c1 = new child2();m1.f1(c1);base b1 = new child2();m1.f2(b1);}}

------------------------------------------------------------ Split line of the extended description -----------------------------------------------------------------------

In fact, strictly speaking, the above application is not rigorous, because although the parent class is the same, the Child class can be expanded freely after inheriting the parent class; it is possible that the methods contained by Child and child2 are not completely consistent, so that the method can be directly replaced with equivalent values. F2 may encounter an exception that cannot be found during the call. So the best case is that child and child2 inherit from the same interface rather than the same class, so that you can avoid calling errors, there is no need to consider the differences between the two replacement classes, as long as the new class can meet the new requirements.

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.