Java uses two examples to fully describe the extensibility of polymorphism _java

Source: Internet
Author: User
Tags abstract

Below I will take two examples to fully explain the scalability of Java, as a good programmer, you must understand the scalability of the program, very conducive to the development of the program follow-up.

First example:

Using the computer's motherboard as an example, we are familiar with the computer's motherboard, there is not a lot of modules, network cards, sound cards, want to use these functions, only when the motherboard to run, and then to the network card and sound card power can be, but this use of software language how to achieve it? is not the first to run the motherboard, the motherboard to let the sound card or network card running up, but to do so, the expansion is not high so that the sound card and network card to run the operation is repeated, and there are many modules have such a function, simply let the motherboard on the module to run, this is one, in case, that day, the technology update, the new module appears, Do you want to create a module object, then run up again, this is obviously not conducive to development, but for this problem, the motherboard manufacturer and sound card, the manufacturer of the network card, after consultation with a PCI interface to connect the motherboard and module, thoroughly resolved the update can not be used or reused problems, And this corresponds to our Java polymorphism, using polymorphism, we can greatly improve the scalability of the program!

The specific code is as follows!

 /* Requirements: computer running instance, computer running based on motherboard.
  * * Interface PCI {public void open ();
public void close ();
  Class Mainboard {public void run () {System.out.println ("mainboard run");
  The public void Usepci (PCI p)//pci p = new netcard ()//interface reference points to its own subclass object.
      {if (p!=null) {p.open ();
      
    P.close ();
  Class Netcard implements PCI {public void open () {System.out.println ("netcard open");
    public void Close () {System.out.println (' netcard close ');
  Method ();
  Class Soundcard implements PCI {public void open () {System.out.println ("soundcard open");
  public void Close () {System.out.println (' soundcard close ');
  }/* Class Mainboard {public void run () {System.out.println ("mainboard run");
    public void Usenetcard (netcard c) {c.open ();
  C.close ();
  } class Netcard {public void open () {System.out.println ("netcard open"); public void Close () {System.out.println("netcard close");
    } */class DuoTaiDemo5 {public static void main (string[] args) {mainboard mb = new mainboard ();
    Mb.run ();
    MB.USEPCI (NULL);
    Mb.usepci (New netcard ());
    
  Mb.usepci (New Soundcard ());
 }
}

A second example:

Using a common database to explain polymorphism, in our well-known database, there are two types of database connection mode: JDBC and Hibernatelian connection, using the database, there are two essential operations, connection and shutdown, at this time, using the interface, the two operations are encapsulated, You need to use that connection mode to replace the class name directly!

The specific code is as follows!

/* Requirements: operation of the database.
The data is: User information. 1, connect to the database.
  JDBC Hibernate 2, Operation database.
C Create R read u update d delete 3 to close the database connection.

  * * Interface Userinfodao {public void Add (user user);
public void Delete (user user);
    Class Userinfobyjdbc implements Userinofdao {public void Add (user user) {1,JDBC connection database.;
    2, add the data using the SQL Add statement.;
  3, close the connection.
    public void Delete (user user) {1,JDBC connection database.;
    2, use SQL Add statement to delete data.;
  3, close the connection.
    The class Userinfobyhibernate implements Userinfodao {public void Add (user user) {1,hibernate connection database.;
    2, add the data using the SQL Add statement.;
  3, close the connection.
    public void Delete (user user) {1,hibernate connection database.;
    2, use SQL Add statement to delete data.;
  3, close the connection.    } class Dboperate {public static void main (string[] args) {//userinfobyjdbc UI = new Userinfobyjdbc ();
    Userinfobyhibernate UI = new Userinfobyhibernate ();
    Userinfodao UI = new Userinfobyhibernate ();
    Ui.add (user);
  Ui.delete (user); }
}

Summarize:

1. These two examples use interfaces to abstract out repetitive operations, in order to allow different modules to directly and quickly use these operations, direct use of polymorphic upward transformation (see my previous blog), can use the parent class reference to call, after all, whether it is the technical update and have a new module or replace the existing module, We can all use the reference of the parent class to invoke their common operation directly!

2. Not necessarily with the interface, you can use abstract classes, but, the use of interfaces, can be more scalable, later updates, you can directly change the interface, do not have to change the abstract class, and secondly, the use of interfaces, can be more inherited! This is also a convenient place.

The above two examples of Java with a full description of the expansion of polymorphism is small set to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

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.