Note that there are some explanatory texts on larger screens.

plurals
  1. POSafe polymorphism practice?
    primarykey
    data
    text
    <p>I am abit unsure what to title this question as, so other people might have use of it, but I have this simple example of polymorphism from my teacher, which I attempted to modify abit.</p> <p>But I am unsure whether my modification is "safe" or not.</p> <pre><code>public class AppSystem { ... private DataPersistenceInterface DataDAO; private DataController DataController; ... public void createConnection(String username, String password) throws ClassNotFoundException, SQLException { if(username.isEmpty() || password.isEmpty()) { DataDAO = new DataDAO(); DataController = new DataController(DataDAO); } else { DataDAO = new DataDAO(url, username, password, driver); DataController = new DataController(DataDAO); } } public void closeConnection() { DataDAO.closeConnection(); } </code></pre> <p>We have a controller and an DAO. The DAO implements an interface called DataPersistenceInterface, which hosts a few methods needed to do some communication with the database. Since the datacontroller handles all the logic, and we don't want it to know about anything else, we pass it a reference of the DAO in the type of the interface.</p> <p>This is what my teacher did. However, the below method "closeConnection" would not work because of this, since the reference to DataDAO does not point to any "closeConnection" method in the DAO class...</p> <p><em>[closeConnection method does not work in the above code, obviously]</em></p> <p>Now, my idea was to just change...</p> <pre><code> private DataPersistenceInterface DataDAO; </code></pre> <p>to</p> <pre><code> private DataDAO DataDAO; </code></pre> <p>Since the datacontroller takes an argument of DataPersistenceInterface in it's constructor, it doesn't get to know about anything else from the DAO object. And now I would be able to call closeConnection on the DAO.</p> <p>But I am not sure if this is "safe" to do? Something just tells me it isn't.</p> <p>Thank you for your time.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload