HƯỚNG DẪN Lack of OOPL understanding II

Joe

Thành viên VIP
21/1/13
3,068
1,343
113
Hi
A JAVA newbie asked a question Hiểu lầm thường gặp về Abstraction trong OOP and a bunch of gurus of this forum explained the poor newbie what Abstraction is and means -even with some links to blogs about Abstraction in English. Maybe my Vietnamese is not good enough so that I am too confused to understand the explanation of the forum gurus about OOP Abstraction. Probably that the gurus themselves are in confusion with the OOP Abstraction too. In OOP there are two types of Abstraction:
  1. Abstract Object with abstract (or unimplemented) and implemented methods
  2. Interface with unimplemented methods
First of all: Abstraction is an abstract of an object which exists virtually as a shell or a capsule (hence it is also called an "encapsulation") with some real contents or simply some hints. For example: Human is an abstraction of a body (implemented) and a gender (abstract). An object Man or Woman is an implementation of abstraction Human where the implemented body (head, body, extremities, etc.) are common (or ready for use) and an abstract gender which must be implemented to complete the object Man or Woman.

The first abstraction or Abstract Object is, as said, an object with an implemented part (i.e. methods) which can be immediately used without modification (or overwritten) and an abstract part which must be individually implemented. Example: Object InputStream is an abstract object whose abstract method read() must be implemented by its children which could be FileInputStream, ByteArrayInputStream, etc.

The second abstraction or an Interface is a shell or a capsule WITHOUT content, but a clutch of predefined methods that must be implemented by its implementers. Example: all Event interfaces have several methods which are implemented by the implementer.

Why two Abstractions? JAVA with its SINGLE inheritance paradigm won't allow an object that is an implementation of different objects. With interface a JAVA object can be an extension of ONE object, but an implementer of several interfaces. In other words: interface is the backdoor of JAVA to escape the SINGLE inheritance restriction. Example:
Java:
public class MyInputStream extends InputStream {
    private byte[] cache;
    private int pos = 0;
    //
    public MyInputStream(byte[] array) {
       cache = array;
    }
    // implement the abstract method read()
    public int read()  throws IOException {
       if (pos >= cache.length) return -1; // EOF
       return ((int)cache[pos++]  & 0xFF);
    }
    ....
}
And an inplementation of an interface ActionListener (with MyInputStream as the implemented abstract class InputStream)
Java:
public class MyApp extends JFrame implements ActionListener {
    private MyInputStream mis;
    private JTextField jtxt;
    ....
    // implement the method required by interface ActionListener
    public void actionPerformed(ActionEvent e) {
      try {
        mis = new MyInputStream(java.nio.Files.readAllBytes((new File(jtxt.getText())).toPath());
        for (int b = mis.read(); b != -1; b = mis.read()) System.out.println("Read:"+b);
        mis.close();
      } catch (Exception ex) {
         if (mis != null) try {
            mis.close();
        } catch (Exception ei) { }
    }
    ...
}
 
Sửa lần cuối: