Clarify Java interfaces and inheritance mechanisms

Source: Internet
Author: User

Author: JavaResearch

Most people think that the meaning of an interface is to replace multiple inheritance. As we all know, Java does not have the multi-Inheritance Mechanism like c ++, but it can implement multiple interfaces. In fact, this is very far-fetched. interfaces and inheritance are completely different. Interfaces do not have the ability to replace multiple inheritance, and they do not have this obligation. The function of an interface, in a word, is the type of class ). You can better manage different types of classes by assigning them to different interfaces. In my opinion, the essence of Java is the abstraction of objects, and interfaces can best reflect this. Why do we discuss that the design patterns are only for languages with abstract capabilities (such as c ++, java, and c #) because the design patterns are studied, it is actually how to reasonably abstract.

The most basic design pattern is the Factory pattern. In a very simple application recently, I want to try to port my program among multiple databases. Of course, this involves many problems. It is a headache to be compatible with the SQL statements of different DBMS. We may want to simplify the problem and only consider how to connect to different databases.

Suppose I have many classes, namely Mysql. java, SQLServer. java, Oracle. java and DB2.java connect to different databases, and return a Connection object in a unified manner. They all have a close method to close the Connection. You only need to select different classes for your DBMS. But what database does my user use? I don't know. I want to modify the code as little as possible to meet his needs. I can abstract the following interfaces:

       
        package org.bromon.test;public interface DB{  java.sql.Connection openDB(String url,String user,String password);  void close();}
       



This interface only defines two methods without any actual code. The specific code is provided by the class implementing this interface, such as Mysql. java:

       
        
Package org. bromon. test; import java. SQL. *; public class Mysql implements DB {private String url = "jdbc: mysql: localhost: 3306/test"; private String user = "root"; private String password = ""; private Connection conn; public Connection openDB (url, user, password) {// database Connection code} public void close () {// close database} similar to Oracle, of course. java, etc. The interface DB gives these classes a class. In the application, we define the object: org. bromon. test. DB myDB;
       



When using myDB to operate databases, you don't have to worry about which class I actually use. This is the so-called "Open-Close" principle. However, the problem is that the interface cannot be instantiated. myDB = new DB (). Such code is absolutely incorrect. We can only use myDB = new Mysql () or myDB = new Oracle (). The problem is that I still need to specify the class to be instantiated, and the interface is useless. So we need a factory:

       
        package org.bromon.test;public class DBFactory{  public static DB Connection getConn()  {    Return(new Mysql());  }}
       



So the instantiated code becomes: myDB = DBFactory. getConn (); this is the most basic Factory in the 23 modes. The Factory class is responsible for the specific class to be instantiated, and other program logic is to operate on the DB interface, this is "programming for interfaces ". Responsibility has been shirked to the Factory class. Of course, you can continue to define the Factory interface and continue to throw the responsibility, which will evolve into Abstract Factory ).

The interface is not responsible for any specific operations throughout the process. To connect other programs to the database, you only need to construct a DB object regardless of how the factory class changes. This is the meaning of the interface ---- abstraction. It is easy to understand the concept of inheritance. Why inherit? Because you want to reuse the code? This is definitely not a reason. The significance of inheritance also lies in abstraction, rather than code reuse. If object A has A run () method, object B also wants to use this method, so someone will use Class B extends. This is a brainless approach. If we instantiate A in B and call the Run () method of A, can we achieve the same purpose? As follows:

       
        Class B{  A  a=new A();  a.run();}
       



This is to reuse code by using class aggregation, which is the prototype of the delegate mode. So what is the significance of inheritance? In fact, this is caused by historical reasons. Please note that inheritance is intended to be abstract rather than code reuse (although inheritance also plays this role ), this is one of the easiest and most serious errors involved in Java. The shadow caused by this mistake during programming may have a serious impact on your programming career. When should I use inheritance? It is used only in abstract classes, and should not be used in other cases. Abstract classes cannot be instantiated. They only provide a template, which can be used to illustrate the problem.

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.