I am having trouble understanding how an interface could improve my app.

I have a DAO class for each profile that logs into the app.this DAO class will know how to insert,delete,update,remove,etc concerning that profile and the database and populating components with database data .

I introduced an Interface for each. I understand it will control or standardize the code for these DAO by baving to implement the methods in the Interface.

Is that it? I have not been able call these Interface elements for anything.
Could someone give me an example how else an interface is used?
thanks

It is used to provide a consistent well, interface, for accessing an object WITHOUT regard to how the actual methods in the interface are implemented. This allows you to swap one class that implements an interface with another that implements it a different way without breaking the underlying logic of the program.

Simple example is the List interface:
A list can be implemented using an array or using a series of nodes linked together. If you want to switch from the array based list to the node based list you can easily do this if they both use the same interface.

I am still trying to understand if I will be able to benefit from building this structure.
I tentatively understand interfaces as a class that implements several interfaces know how to do all the things it implements. If a class extends a class it inherits data members and methods of the class it extends.

I have a class ConnectStudentDAO.java that knows how to connect to a db and get any resultset needed to return any combination of data dealing with a Student profile.

I have a StudentPanal (GUI) that I would like to know how to do everything ConnectStudentDAO can do.


I can build my StudentPanel now by Calling ConnectStudentDAO cad =new ConnectStudentDAO() over and over.
so, is an interface more effective?

right now I have a class StudentDAO (interface) that only has methods with no bodies and
ConnectStudentDAO implements this class. but it is useless.
Thanks

You might not see the benefit of programming to interfaces in a small program but it is good practice.

Say you want a different implementation of how you connect to the database. You'd create a new class that implements StudentDAO and replace references of ConnectStudentDAO with the new implementation:

StudentDAO connection = new ConnectStudentDAO();
StudentDAO connection = new ADifferentConnectStudentDAO();

Notice that I'm using StudentDAO (the interface ) as the type.

As it stands, I'd make the GUI a seperate class from the database connection and encapsulate an object of the database connection in the GUI:

public MyGUI{

private StudentDAO connection = new ConnectStudentDAO();

...
connection.getResultSet() and so on...
...
...
...
}

That sound like a good structure. It is getting clearer thanks
I have my StudentDAO (interface) and my ConnectStudentDAO processes all queries conn, etc
so it should implement StudentDAO(interface)

All the interface allows is calls to methods with no body but it does allow the passing of an arg. Is this arg to be the instance ConnectStudentDAO connectStudentDAO so the new method of connection can step in and takeover by using StudentDAO.

IF SO, Should any method I create in ConnectStudentDAO start in StudentDAO?

because right now I never call StudentDAO because I just go right ot ConnectStudentDAO.

public interface StudentDAO {
    
    public Student connect(ConnectStudentDAO connectStudentDAO)
            throws LoginException,
            //UnknownstudentUserException,
            IncorrectPasswordException;
/*
    public void insertStudent(Student studentUser)
            throws SubscribeException;

    public void update(Student studentUser)
            throws ProfileException;

    public void delete(Student studentUser)
            throws UnsubscribeException;
 * 
 */
}

IF SO, Should any method I create in ConnectStudentDAO start in StudentDAO?

Not necessarily.

public interface StudentDAO {
   // This sort of thing would make more sense to me.  
   public Student connect( String user, String host, String password )
             throws LoginException, IncorrectPasswordException;

   //public Student connect(ConnectStudentDAO connectStudentDAO)
   //         throws LoginException,
   //         //UnknownstudentUserException,
   //         IncorrectPasswordException;
/*
    public void insertStudent(Student studentUser)
            throws SubscribeException;
 
    public void update(Student studentUser)
            throws ProfileException;
 
    public void delete(Student studentUser)
            throws UnsubscribeException;
 * 
 */
}

How can I incororate the interface? this works now but I don't see the interface in play.

if (profile.equals("admin_")) {
                    Admin admin = new Admin();
                    DerbyDAOFactory f =new DerbyDAOFactory();
                    [b]ConnectAdminDAO adminDAO=(ConnectAdminDAO)f.getAdminUserDAO();
                    AdminBean adminBean = adminDAO.select( user,password,profile);
                     [/b]
                    ViewUtils.setAdmin(true);
                    //adminBean.setLoggedIn(true);
                   // ModelUtils.copy(adminBean, admin);

                }
public AdminDAO getAdminUserDAO() throws ClassNotFoundException, InstantiationException, IllegalAccessException, FileNotFoundException, IOException {
        Class daoClass = Class.forName(ModelUtils.getXMLResource("adminDAO"));
        [b]adminDAO = (ConnectAdminDAO) daoClass.newInstance();[/b]
        System.out.println("CREATED NEW INSTANCE OF ConnectAdminDAO");
        //[b]"adminDAO=<entry key="adminDAO">model.dao.ConnectAdminDAO</entry>[/b]
        return adminDAO;
    }
public class ConnectAdminDAO extends LoginInfo implements AdminDAO {
   
    public void ConnectAdmin() throws UnknownUserNameException, SQLException, IncorrectPasswordException, LoginException, FileNotFoundException, IOException {
    }
[b]
    public AdminBean select(String user, char[] password, String profile) throws 
[/b]
UnknownUserNameException, IncorrectPasswordException, LoginException, SQLException, FileNotFoundException, IOException, ProfileException {
        this.profile = profile;
        this.user = user;
        this.password = password;
        setProfile(profile);
        setUser(user);
        setPassword(password);

        DerbyDAOFactory ddf = new DerbyDAOFactory();
        conn = ddf.getConnection();
        testDB();
        ps = conn.prepareStatement(ModelUtils.getXMLResource("adminUserName"));
        ps.setString(1, getUser());
......
public interface AdminDAO {
   /*
     public Admin connect(ConnectAdminDAO connectAdminDAO)
            throws LoginException,
            //UnknownstudentUserException,
            IncorrectPasswordException;
     public Admin select(ConnectAdminDAO connectAdminDAO);

    public void insertAdmin(Admin adminUser)
            throws SubscribeException;

    public void update(Admin adminUser)
            throws ProfileException;

    public void delete(Admin adminUser)
            throws UnsubscribeException;
    public void fillOptionsCB(Admin adminUser)
        throws UnsubscribeException;
*/
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.